Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Getting the correct time of day in 12 hour format?

Really hoping someone here knows. The time stamps in the chat box can do it, so I should be able to as well. In a post, I found these as reference, but they all seem to give military style time: var now = new Date(); var date = now.toDateString(); // Tue Nov 26 2013 var iso = now.toISOString(); // 2013-11-27T01:41:03.900Z var json = now.toJSON(); // 2013-11-27T01:41:03.900Z var lDate = now.toLocaleDateString(); // Tuesday, November 26, 2013 var lTime = now.toLocaleTimeString(); // 7:41:03 PM var locale = now.toLocaleString(); // Tuesday, November 26, 2013 7:41:03 PM var dStr = now.toString(); // Tue Nov 26 2013 19:41:03 GMT-0600 (Central Standard Time) var time = now.toTimeString(); // 19:41:03 GMT-0600 (Central Standard Time) var utc = now.toUTCString(); // Wed, 27 Nov 2013 01:41:03 GMT Is there a way to change them around to standard time, like in the Roll20 tstamp function? is there a way to call that function? This is what I have in my version of the enter/leave script var lTime = now.toLocaleTimeString(); // 7:41:03 PM But what I get is 24 hour version of the server time,22:37:27 GMT-0500 (CDT), not my time zone and not in 12 hour time. Any suggestions?
1437538478
Lithl
Pro
Sheet Author
API Scripter
API scripts run on the API server, and have no access to your computer. They can't know what time zone you're in unless you tell them yourself.
How do I tell it? With no luck, I tried this: var ltime = now.toTimeString('en-US' , { timeZone: 'America/Detroit', hour12:true, hour: 'numeric', minute: '2-digit', pattern: '{hour}:{minute} {ampm}' });
1437539288
The Aaron
Pro
API Scripter
You'll probably need to make a Date object and manipulate the individual parts:     var d=new Date(),         h=d.getHours(),         m=d.getMinutes(),         s=d.getSeconds();
1437539840
Andrew R.
Pro
Sheet Author
I'd like to point out, tongue pressed firmly in cheek, that “standard” time is the output of .toISOString()  I fondly remember convincing a fellow System Administrator to use ISO time stamp strings over his previously preferred version. ;-)
Got it, thanks for the hint! I googled .getHours(); and found someone had done the work for me. var now = new Date(); var ldate = now.toLocaleDateString(); // Tuesday, November 26, 2013 var h1=now.getHours(); var h=h1 + 1 var m=now.getMinutes(); var s=now.getSeconds(); var ap = "AM"; if (h > 11) { ap = "PM"; } if (h > 12) { h = h - 12; } if (h == 0) { h = 12; } if (h < 10) { h = "0" + h; } if (m < 10) { m = "0" + m; } if (s < 10) { s = "0" + s; } var ltime = h + ':' + m + ':' + s + " " + ap;
1437541844

Edited 1437542059
The Aaron
Pro
API Scripter
Probably better to put the extra hour here: var h=((now.getHours()+1)%24); otherwise you might end up with 13:27:24 AM..
1437542045
The Aaron
Pro
API Scripter
Also, h might be the string "03", in which case you'd get 031:27:24 AM instead of 04:27:24 AM. =D