Skip to content

Instantly share code, notes, and snippets.

@hurjas
Forked from jonkemp/timestamp.js
Created May 11, 2012 15:35
Show Gist options
  • Save hurjas/2660489 to your computer and use it in GitHub Desktop.
Save hurjas/2660489 to your computer and use it in GitHub Desktop.
Print out a nicely formatted timestamp in JavaScript.
/**
* Return a timestamp with the format "m/d/yy h:MM:ss TT"
* @type {Date}
*/
function timeStamp() {
// Create a date object with the current time
var now = new Date();
// Create an array with the current month, day and time
var date = [ now.getMonth() + 1, now.getDate(), now.getFullYear() ];
// Create an array with the current hour, minute and second
var time = [ now.getHours(), now.getMinutes(), now.getSeconds() ];
// Determine AM or PM suffix based on the hour
var suffix = ( time[0] < 12 ) ? "AM" : "PM";
// Convert hour from military time
time[0] = ( time[0] < 12 ) ? time[0] : time[0] - 12;
// If hour is 0, set it to 12
time[0] = time[0] || 12;
// If seconds and minutes are less than 10, add a zero
for ( var i = 1; i < 3; i++ ) {
if ( time[i] < 10 ) {
time[i] = "0" + time[i];
}
}
// Return the formatted string
return date.join("/") + " " + time.join(":") + " " + suffix;
}
Copy link

ghost commented May 15, 2018

been super useful! created a fiddle using a variation of @Rundik's code.

@keithpjolley
Copy link

Here's similar using built-ins. I think it may be more portable while still achieving the same goals, except it's yyyy instead of yy, but I much prefer yyyy anyways.

> function timeStamp(date, locale) {
  const event = (date===undefined) ? new Date() : new Date(date);
  return event.toLocaleDateString(locale) + " " + event.toLocaleTimeString(locale)
};

> timeStamp() // no args gives current date+time in users locale (e.g. en-us, PDT)
6/20/2018 2:06:31 PM
> timeStamp(0) // date input can be various formats
12/31/1969 4:00:00 PM
> timeStamp("7/4/1776 6:00 AM", 'en-gb') // give date+time in other locales
04/07/1776 06:00:00
> timeStamp("7/4/1776 6:00 AM", 'en-us') // force the og format in other locales
7/4/1776 6:00:00 AM
> timeStamp(new Date(), 'en-us') // current date in og format even in different locales
6/20/2018 2:06:31 PM

@seox
Copy link

seox commented Aug 29, 2018

Greetings, a more complete function using a more complete String replace function, use: mf("Y-M-D H:I:S")
Y=Complete Year
y=Two digits year
M=Month
D=Date
H:Hour
h:12 hour format
I: minutes
S:seconds
x:am/pm

function mf(formato){ var hor,hor2,x; var fechai=new Date(); var ano=fechai.getYear()+1900; var ano2=(""+(fechai.getYear()+1900)).slice(-2); var mes=("0"+(fechai.getMonth()+1)).slice(-2); var dia=("0"+fechai.getDate()).slice(-2); hor=hor2=("0"+fechai.getHours()).slice(-2); if(fechai.getHours()>12){ hor2=fechai.getHours()-12; x="pm"; }else{ x="am"; } var min=("0"+fechai.getMinutes()).slice(-2); var seg=("0"+fechai.getSeconds()).slice(-2); var fecha=formato.reemplaza("YyMDHhISx".split(''),[ano,ano2,mes,dia,hor,hor2,min,seg,x]); alert(fecha); } String.prototype.reemplaza=function(busca,reemplaza){ var b=Array.isArray(busca); var r=Array.isArray(reemplaza); var ret=this; if(b){ for(var i in busca){ var c=r?(reemplaza[i]?reemplaza[i]:""):reemplaza; ret=ret.replace(new RegExp(busca[i],'g'),c); } }else{ var c=r?reemplaza[0]:reemplaza; ret=ret.replace(new RegExp(busca,'g'),c); } return ret; }

@IceBotYT
Copy link

Thanks a lot!

@IsabelMeraner
Copy link

cool, thanks for sharing your example!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment