如何在JavaScript中格式化日期

Given a Date object:

给定一个Date对象:

const date = new Date('July 22, 2018 07:22:13')

there are lots of methods that will generate a string representing that date.

有很多方法可以生成表示该日期的字符串。

There are a few built-in ones. I list them all, along with a comment that shows a sample output:

有一些内置的。 我列出了所有内容,以及显示示例输出的注释:

date.toString()
// "Sun Jul 22 2018 07:22:13 GMT+0200 (Central European Summer Time)"
date.toTimeString() //"07:22:13 GMT+0200 (Central European Summer Time)"
date.toUTCString() //"Sun, 22 Jul 2018 05:22:13 GMT"
date.toDateString() //"Sun Jul 22 2018"
date.toISOString() //"2018-07-22T05:22:13.000Z" (ISO 8601 format)
date.toLocaleString() //"22/07/2018, 07:22:13"
date.toLocaleTimeString()	//"07:22:13"

You are not limited to those, of course - you can use more low level methods to get a value out of a date, and construct any kind of result you want:

当然,您不限于这些-您可以使用更多的低级方法来获取过期的值,并构造所需的任何结果:

date.getDate() //22
date.getDay() //0 (0 means sunday, 1 means monday..)
date.getFullYear() //2018
date.getMonth() //6 (starts from 0)
date.getHours() //7
date.getMinutes() //22
date.getSeconds() //13
date.getMilliseconds() //0 (not specified)
date.getTime() //1532236933000
date.getTimezoneOffset() //-120 (will vary depending on where you are and when you check - this is CET during the summer). Returns the timezone difference expressed in minutes

Those all depend on the current timezone of the computer. There are equivalent UTC versions of these methods, that return the UTC value rather than the values adapted to your current timezone:

这些都取决于计算机的当前时区。 这些方法有等效的UTC版本,它们返回UTC值,而不是适合您当前时区的值:

date.getUTCDate() //22
date.getUTCDay() //0 (0 means sunday, 1 means monday..)
date.getUTCFullYear() //2018
date.getUTCMonth() //6 (starts from 0)
date.getUTCHours() //5 (not 7 like above)
date.getUTCMinutes() //22
date.getUTCSeconds() //13
date.getUTCMilliseconds() //0 (not specified)

翻译自: https://flaviocopes.com/how-to-format-date-javascript/

你可能感兴趣的:(java,python,javascript,字符串,json,ViewUI)