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

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


当前回答

对于已接受的答案,1月1日将显示如下:2017/1/1。

如果你更喜欢2017/01/01,你可以使用:

var dt = new Date();
var date = dt.getFullYear() + '/' + (((dt.getMonth() + 1) < 10) ? '0' : '') + (dt.getMonth() + 1) + '/' + ((dt.getDate() < 10) ? '0' : '') + dt.getDate();

其他回答

您可以简单地使用这一行代码以年-月-日格式获取日期

var date = new Date().getFullYear() + "-" + new Date().getMonth() + 1 + "-" + new Date().getDate();

info

如果需要2位数字的月份和日期(2016/01/01 vs 2016/1/1)

code

var dateObj = new Date();
var month = ('0' + (dateObj.getMonth() + 1)).slice(-2);
var date = ('0' + dateObj.getDate()).slice(-2);
var year = dateObj.getFullYear();
var shortDate = year + '/' + month + '/' + date;
alert(shortDate);

输出

2016/10/06

小提琴

https://jsfiddle.net/Hastig/1xuu7z7h/

信贷

更多信息来自这个答案

more

要了解更多关于.slice的信息,w3schools的自己尝试编辑器帮助我更好地了解如何使用它。

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()