简体   繁体   English

Javascript Date toString转换并格式化为本地时间

[英]Javascript Date toString convert and format to local time

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)

script: 脚本:

$(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());
    })
})

How would I format the toString so the output was something like this? 我将如何格式化toString以便输出是这样的?

Thu Jun 15 2017 5:00:00 PM Eastern Daylight Time

You'd have to parse it yourself. 您必须自己解析它。 There are libraries that can do it as well (Stack Overflow rules advise not making recommendations, but if you Google "javascript date format library" a bit you'll find them), but if you want to do it with plain JavaScript, you'll just need to build the string yourself. 有一些库也可以做到这一点(Stack Overflow规则建议不要提出建议,但是如果您使用Google“ javascript日期格式库”,会发现它们),但是如果您要使用纯JavaScript来实现,我只需要自己构建字符串即可。

Since you're already creating a Date object, you're half way there. 由于您已经在创建Date对象,因此您已经完成了一半。

The Date object only has numbers, so it won't have strings for things like Thu , Jun and Eastern Daylight Time . Date对象只有数字,因此不会包含ThuJunEastern Daylight Time类的字符串。 For those, you'll need lists of possible values and to map the numbers to them (usually a simple array will work). 对于这些,您将需要列出可能的值并将其映射到它们(通常可以使用一个简单的数组)。

For the 5:00:00 PM part, you can mod ( % ) the hours by 12, and then specify AM if hours < 12 or PM if hours >= 12 . 对于5:00:00 PM部分,可以MOD( % 12)小时,然后指定AM如果hours < 12PM如果hours >= 12

const time = (d.getHours() % 12) + ':' + d.getMinutes() + ':' + d.getSeconds() + (d.getHours() < 12 ? 'AM' : 'PM');

I won't give you the full JavaScript because you should take a stab at writing it yourself. 我不会提供完整的JavaScript,因为您应该自己动手编写脚本。 If you have any trouble, please ask another question. 如果您有任何麻烦,请再问一个问题。

You can also use combinations of toLocaleString() , but it won't do everything you need (it doesn't do 12-hour date format, for example). 您也可以使用toLocaleString()组合,但是它不能满足您的所有需求(例如,它不执​​行12小时日期格式)。

If you can use an external library, I'd suggest this one liner using momentjs : 如果您可以使用外部库,我建议使用momentjs使用此库:

moment('2017-06-15 21:00').format('ddd MMM DD YYYY h:mm:ss A')
// => "Thu Jun 15 2017 9:00:00 PM"

Using @James suggestion to use toLocaleString() 使用@James建议使用toLocaleString()

$(document).ready(function() {
    $('.plg-date > .fabrikElement > div').each(function() {
        if($(this).text().length > 0) {
            var date = $(this).text();
            var newdate = new Date(date + " UTC");
            var options = { 
                year: 'numeric', 
                month: 'numeric', 
                day: 'numeric',
                hour: '2-digit',
                minute: '2-digit',
                timeZoneName: 'short'
            }
            $(this).text(newdate.toLocaleString('en-US', options)); 
        }
    })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM