Javascript Date toString转换并格式化为本地时间 [英] Javascript Date toString convert and format to local time
本文介绍了Javascript Date toString转换并格式化为本地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个脚本,该脚本以以下格式获取日期/时间:
I have a script that gets a date/time in the the format of:
2017-06-15 21:00
,然后将其转换为本地时间,并显示为:
and then converts it to local time and displays as:
Thu Jun 15 2017 17:00:00 GMT-0400 (Eastern Daylight Time)
脚本:
$(document).ready(function() {
$('.plg-date > .fabrikElement > div').each(function() {
var date = $(this).text();
var newdate = new Date(date + " UTC");
console.log(newdate);
$(this).text(newdate.toString());
})
})
我应该如何格式化toString以便输出是这样的?
How would I format the toString so the output was something like this?
Thu Jun 15 2017 5:00:00 PM Eastern Daylight Time
推荐答案
If you can use an external library, I'd suggest this one liner using momentjs:
moment('2017-06-15 21:00').format('ddd MMM DD YYYY h:mm:ss A')
// => "Thu Jun 15 2017 9:00:00 PM"
这篇关于Javascript Date toString转换并格式化为本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文