Javascript Date toString convert and format to local time

后端 未结 3 2018
囚心锁ツ
囚心锁ツ 2021-01-28 02:37

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:



        
3条回答
  •  情歌与酒
    2021-01-28 03:01

    Using @James suggestion to use 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)); 
            }
        })
    })
    

提交回复
热议问题