Last active
November 30, 2015 03:50
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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