admin管理员组文章数量:1277509
How to format date and time like this in JavaScript ?
March 05, 2012 @ 14:30 (UTC - 9:30)
I use this code to calculate EST time :
function getDate() {
var now = new Date();
var utc = now.getTime() + (now.getTimezoneOffset() * 60000);
return new Date(utc + (3600000 * -4));
}
How to format date and time like this in JavaScript ?
March 05, 2012 @ 14:30 (UTC - 9:30)
I use this code to calculate EST time :
function getDate() {
var now = new Date();
var utc = now.getTime() + (now.getTimezoneOffset() * 60000);
return new Date(utc + (3600000 * -4));
}
Share
Improve this question
asked Mar 14, 2012 at 20:43
ShahinShahin
12.9k40 gold badges129 silver badges208 bronze badges
3
- developer.mozilla/en/JavaScript/Reference/Global_Objects/… – Corbin Commented Mar 14, 2012 at 20:44
- 3 Hiya, hope this helps, - blog.stevenlevithan./archives/date-time-format , cheers – Tats_innit Commented Mar 14, 2012 at 20:45
- Possible duplicate of How to format a JavaScript date – John Slegers Commented Feb 19, 2016 at 21:45
3 Answers
Reset to default 4I use the date-time-format that Tats remended because doing it manually is a huge PIA.
var yourDate = dateFormat(getDate(), "mmmm dd, yyyy @ HH:MM) + "(UTC -9:30)";
Keep in mind this isn't Daylight Savings aware.. and you are asking for UTC -9:30 in your format, but your function converts to -4. Also, I believe that now.getTime returns in UTC.. so you can just add your difference there.
JavaScript Date Format
Check out date.js! It's a really powerful little library for working with Dates in JavaScript.
To get today's date in EST, you can do something like...
var today = new Date();
today.toString(); // outputs "Wed Apr 11 2012 15:40:40 GMT-0500 (CDT)"
today.setTimezone("EST");
today.toString(); // outputs "Wed Apr 11 2012 14:40:40 GMT-0500 (CDT)"
Also, its worth mentioning to checkout moment.js. I think the two libraries plement each other.
If you do just
var now = new Date();
document.write(now);
you will get
Wed Mar 14 2012 20:53:06 GMT+0000 (GMT Standard Time)
Link1, Link2.
Is it what you want?
本文标签: Format date time JavaScriptStack Overflow
版权声明:本文标题:Format date time JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741294517a2370738.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论