-
-
Save hurjas/2660489 to your computer and use it in GitHub Desktop.
/** | |
* 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; | |
} |
Thanks. This saved me a couple of minutes so I decided to spent them on dropping a line to say cheers :-)
Thanks @hurjas
Thanks a lot, Its working , thanks !!
(new Date).toISOString();
"2015-07-03T10:49:26.892Z"
(new Date).toISOString().replace(/z|t/gi,' ');
"2015-07-03 10:50:22.481 "
(new Date).toISOString().replace(/z|t/gi,' ').trim();
"2015-07-03 10:50:31.112"
Coming late to the party, but this snippet was helpful to me... So thanks!
I think you should be able to save a step if you test for the hour being greater than or equal to 12 when converting from military time.
// 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; <--- No longer needed
Very nice.
I needed a compact date/time format -- don't care about seconds, don't care about other centuries -- so I modified the top lines to look like the following. If no options are give, it works as original.
function timeStamp(options) {
options = typeof options !== 'undefined' ? options : {};
// 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() ];
if(options.compactDate) {
date.push(now.getFullYear()-2000);
}
else {
date.push(now.getFullYear());
}
// Create an array with the current hour, minute and second
var time = [ now.getHours(), now.getMinutes() ];
if(!options.compactTime) {
time.push(now.getSeconds());
}
// Determine AM or PM suffix based on the hour
.
.
.
When I call it with timeStamp({compactTime:true, compactDate:true})
I get something like "9/18/15 4:18 PM".
Thanks for this, was very helpful
👍
Thanks!
good job!! 👍
hwo do I call this into a string?
Super helpful, thanks!
Thank you a lot!
Huge help! Cheers!
Thanks
Nice! ;)
Just what I needed.... hugs!!!
Works great! Thanks!
Thanks! Works great. Saved me some time.
function timeStamp() {
// Create a date object with the current time
let now = new Date();
// Create an array with the current month, day and time
let date = [ now.getMonth() + 1, now.getDate(), now.getFullYear() ].map(d=>d.toString().length === 1 ? "0"+d : d);
// Create an array with the current hour, minute and second
let time = [ now.getHours(), now.getMinutes(), now.getSeconds() ].map(d=>d.toString().length === 1 ? "0"+d : d);
// Return the formatted string
return time.join(":") + " " + date.join(".");
}
Thanks Rundik.
Me like it too!
Great!
Extremely handy. Thanks!!
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
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; }
Thanks a lot!
cool, thanks for sharing your example!
It's really nice, thanks! 👍