警报(dateObj)给出周三2009年12月30日00:00:00 GMT+0800

如何获得日期格式为2009/12/30?


当前回答

var dt = new Date();

dt.getFullYear() + "/" + (dt.getMonth() + 1) + "/" + dt.getDate();

因为月份索引是以0为基础的,所以必须加1。

Edit

有关日期对象函数的完整列表,请参见

Date

getMonth()

根据当地时间返回指定日期中的月份(0-11)。

getUTCMonth()

根据世界时返回指定日期中的月份(0-11)。

其他回答

为什么不使用方法toISOString()与切片或简单的toLocaleDateString()?

注意toISOString返回的时区总是零UTC偏移量,而在toLocaleDateString中它是用户代理的时区。

检查:

const d = new Date() //今天,现在 //时区0 UTC偏移量 console.log (d.toISOString()。slice(0, 10)) // YYYY-MM-DD //用户代理的时区 console.log(d.toLocaleDateString('en-CA')) // YYYY-MM-DD .log(d.toLocaleDateString('en-CA') console.log(d.toLocaleDateString('en-US')) // M/D/YYYY .log(d.toLocaleDateString('en-US') console.log(d.toLocaleDateString('de-DE')) // d.m.y yyyy .log console.log(d.toLocaleDateString('pt-PT')) // DD/MM/YYYY .log(d.toLocaleDateString('pt-PT')

var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();

newdate = year + "/" + month + "/" + day;

或者你可以设置新的日期并给出上面的值

var dt = new Date();

dt.getFullYear() + "/" + (dt.getMonth() + 1) + "/" + dt.getDate();

因为月份索引是以0为基础的,所以必须加1。

Edit

有关日期对象函数的完整列表,请参见

Date

getMonth()

根据当地时间返回指定日期中的月份(0-11)。

getUTCMonth()

根据世界时返回指定日期中的月份(0-11)。

let dateObj = new Date();

let myDate = (dateObj.getUTCFullYear()) + "/" + (dateObj.getMonth() + 1)+ "/" + (dateObj.getUTCDate());

作为参考,你可以看到下面的细节

new Date().getDate()          // Return the day as a number (1-31)
new Date().getDay()           // Return the weekday as a number (0-6)
new Date().getFullYear()      // Return the four digit year (yyyy)
new Date().getHours()         // Return the hour (0-23)
new Date().getMilliseconds()  // Return the milliseconds (0-999)
new Date().getMinutes()       // Return the minutes (0-59)
new Date().getMonth()         // Return the month (0-11)
new Date().getSeconds()       // Return the seconds (0-59)
new Date().getTime()          // Return the time (milliseconds since January 1, 1970)

let dateObj = new Date(); let myDate = (dateObj.getUTCFullYear()) + “/” + (dateObj.getMonth() + 1)+ “/” + (dateObj.getUTCDate()); console.log(myDate)

它将从用户的浏览器设置中收集语言

使用选项对象中的minutes和hour属性来处理它们。 你可以用长值来表示月份,比如8月23日等等。

function getDate(){
 const now = new Date()
 const option = {
  day: 'numeric',
  month: 'numeric',
  year: 'numeric'
 }
 const local = navigator.language
 labelDate.textContent = `${new 
 Intl.DateTimeFormat(local,option).format(now)}`
}
getDate()