如何更改ISOString的日期格式? [重复]

时间:2022-07-17 02:27:29

This question already has an answer here:

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

How can I change the output to be more readable? from this 2015-04-25T00:00:00Z to 2015-04-2 00:00:00-00:00:00

如何更改输出更易读?从2015-04-25T00:00:00Z到2015-04-2 00:00:00-00:00:00

I am using this (new Date()).toISOString()

我正在使用它(new Date())。toISOString()

2 个解决方案

#1


Dates are very strange beast in javascript. If at all possible I would highly recommend using a date library like momentjs to handle this for you. Using moment this would be as simple as:

日期是javascript中非常奇怪的野兽。如果可能的话,我强烈建议您使用像momentjs这样的日期库来为您处理。使用时刻这将是如此简单:

var formattedDate = moment().format('YYYY-MM-DD hh:mm:ss')

var formattedDate = moment()。format('YYYY-MM-DD hh:mm:ss')

#2


This is one way to handle it in JavaScript. You get the Date and then the various parts. Then you concatenate them together into a variable to use as you wish.

这是在JavaScript中处理它的一种方法。你得到日期,然后是各个部分。然后将它们连接成一个变量,以便根据需要使用。

var d = new Date();
var dy = d.getDate();
var dmth = d.getMonth();
var dyr = d.getFullYear();
var dFullDate = dyr+ '/' +dmth+ '/' +dy;

#1


Dates are very strange beast in javascript. If at all possible I would highly recommend using a date library like momentjs to handle this for you. Using moment this would be as simple as:

日期是javascript中非常奇怪的野兽。如果可能的话,我强烈建议您使用像momentjs这样的日期库来为您处理。使用时刻这将是如此简单:

var formattedDate = moment().format('YYYY-MM-DD hh:mm:ss')

var formattedDate = moment()。format('YYYY-MM-DD hh:mm:ss')

#2


This is one way to handle it in JavaScript. You get the Date and then the various parts. Then you concatenate them together into a variable to use as you wish.

这是在JavaScript中处理它的一种方法。你得到日期,然后是各个部分。然后将它们连接成一个变量,以便根据需要使用。

var d = new Date();
var dy = d.getDate();
var dmth = d.getMonth();
var dyr = d.getFullYear();
var dFullDate = dyr+ '/' +dmth+ '/' +dy;