警报(dateObj)给出周三2009年12月30日00:00:00 GMT+0800
如何获得日期格式为2009/12/30?
警报(dateObj)给出周三2009年12月30日00:00:00 GMT+0800
如何获得日期格式为2009/12/30?
当前回答
使用Date获取方法。
http://www.tizag.com/javascriptT/javascriptdate.php
http://www.htmlgoodies.com/beyond/javascript/article.php/3470841
var dateobj= new Date() ;
var month = dateobj.getMonth() + 1;
var day = dateobj.getDate() ;
var year = dateobj.getFullYear();
其他回答
下面是一个使用模板字面值获取年/月/日的更简洁的方法:
var 日期 = 新日期(); var formattedDate = '${date.getFullYear()}/${(date.getMonth() + 1)}/${date.getDate()}'; 控制台.log(格式化日期);
使用Date获取方法。
http://www.tizag.com/javascriptT/javascriptdate.php
http://www.htmlgoodies.com/beyond/javascript/article.php/3470841
var dateobj= new Date() ;
var month = dateobj.getMonth() + 1;
var day = dateobj.getDate() ;
var year = dateobj.getFullYear();
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)
不错的格式化插件:http://blog.stevenlevithan.com/archives/date-time-format。
你可以这样写:
var now = new Date();
now.format("yyyy/mm/dd");
我建议你使用Moment.js http://momentjs.com/
然后你可以这样做:
moment(new Date()).format("YYYY/MM/DD");
注意:如果你想要当前的TimeDate,你实际上不需要添加新的Date(),我只添加它作为一个引用,你可以传递一个日期对象给它。对于当前的TimeDate,这也适用:
moment().format("YYYY/MM/DD");