如何从javascript中的UTC字符串中获取(MM-DD-YYYY)格式? [重复]

时间:2023-02-05 19:44:15

This question already has an answer here:

这个问题在这里已有答案:

I am working with a Date object in a different timezone than the console. I'm using utc string to get the actual time that was logged in that timezone. Is there an easy way to extract the (MM-DD-YYYY) from the utc string than just substringing into the string?

我正在使用与控制台不同的时区中的Date对象。我正在使用utc字符串来获取在该时区中记录的实际时间。有没有一种简单的方法从utc字符串中提取(MM-DD-YYYY)而不仅仅是子串入字符串?

isoString = "2016-08-25T15:17:21.033-10:00"
utc= (new Date(isoString)).toUTCString()

Returns: "Fri, 26 Aug 2016 01:17:21 GMT"

返回:“星期五,2016年8月26日01:17:21 GMT”

Would like (08-26-2016)

想(08-26-2016)

1 个解决方案

#1


0  

You would convert it into a Date Object and then try out the methods available in it to get the required format.

您可以将其转换为日期对象,然后尝试其中可用的方法以获得所需的格式。

var isoString = "2016-08-25T15:17:21.033-10:00"
var utc= (new Date(isoString)).toUTCString()

console.log(new Date(utc).getDate() + "-" + (new Date(utc).getMonth() + 1) +
                                                 "-" + new Date(utc).getFullYear());

#1


0  

You would convert it into a Date Object and then try out the methods available in it to get the required format.

您可以将其转换为日期对象,然后尝试其中可用的方法以获得所需的格式。

var isoString = "2016-08-25T15:17:21.033-10:00"
var utc= (new Date(isoString)).toUTCString()

console.log(new Date(utc).getDate() + "-" + (new Date(utc).getMonth() + 1) +
                                                 "-" + new Date(utc).getFullYear());