Skip to content

Instantly share code, notes, and snippets.

@k4kfh
Last active November 30, 2015 03:50
Show Gist options
  • Save k4kfh/a3d51e23d8b46360f329 to your computer and use it in GitHub Desktop.
Save k4kfh/a3d51e23d8b46360f329 to your computer and use it in GitHub Desktop.
Simple timestamp system in JavaScript; returns current time in hr:min:sec format
timestamp = function(type) {
if (type == undefined) {
type = 24;
}
if (type == 24) {
var currentDate = new Date();
var hours = currentDate.getHours();
var minutes = currentDate.getMinutes();
var seconds = currentDate.getSeconds();
var finalString = hours + ":" + minutes + ":" + seconds;
return finalString;
}
else if (type == 12) {
var currentDate = new Date();
var hours = currentDate.getHours();
if (hours == 24) {
hours = 12;
ampm = "AM"
}
if (hours > 12) {
hours = hours - 12;
ampm = "PM"
}
if (hours == 12) {
hours = 12;
ampm = "PM"
}
var minutes = currentDate.getMinutes();
var seconds = currentDate.getSeconds();
var finalString = hours + ":" + minutes + ":" + seconds + " " + ampm;
return finalString;
}
}
//Call with timestamp(12) or timestamp(24)
//See this post for more information: http://evilgeniustech.com/easy-javascript-timestamp/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment