如何从日期对象获取年/月/日?

use*_*729 215 javascript date

alert(dateObj)Wed Dec 30 2009 00:00:00 GMT+0800

如何获取格式的日期2009/12/30

Hiy*_*sat 365

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;
Run Code Online (Sandbox Code Playgroud)

或者您可以设置新日期并提供上述值

  • 请记住:1月= 0,2月= 1,依此类推. (66认同)
  • `getUTCDay()`应替换为`getUTCDate()`,因为day是星期几(0-6)的天数,date是月中的天数(1-31). (53认同)
  • UTC将返回通用时间.如果你想让当地时间使用getMonth (10认同)
  • `getMonth`和`getUTCMonth`有什么区别? (8认同)
  • 我认为这种回应仍然存在缺陷。getUTCDate()确实会返回该月的某天,但在英国。例如,如果我键入:var d = new Date(“ 1983年7月21日01:15:00”); d.getDate()返回21,但d.getUTCDate()仅返回20,这是因为在法国(我在这里)的早晨01:15,在英国仍然是23:15。要获取原始日期中的日期,应使用getDate()。 (3认同)

rah*_*hul 96

var dt = new Date();

dt.getFullYear() + "/" + (dt.getMonth() + 1) + "/" + dt.getDate();
Run Code Online (Sandbox Code Playgroud)

由于月份指数为0,因此您必须将其递增1.

编辑

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

日期

getMonth()
Run Code Online (Sandbox Code Playgroud)

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

getUTCMonth()
Run Code Online (Sandbox Code Playgroud)

根据通用时间返回指定日期的月份(0-11).

  • 需要parens.否则十月将显示为91个月(因为9 + 1 = 91 in stringland) (5认同)
  • 应该是dt.getFullYear()+"/"+(dt.getMonth()+ 1)+"/"+ dt.getDate(); (5认同)

dav*_*rey 79

new Date().toISOString()
"2016-02-18T23:59:48.039Z"
new Date().toISOString().split('T')[0];
"2016-02-18"
new Date().toISOString().replace('-', '/').split('T')[0].replace('-', '/');
"2016/02/18"

new Date().toLocaleString().split(',')[0]
"2/18/2016"
Run Code Online (Sandbox Code Playgroud)

  • 我认为这对于第三种情况可能会更好 new Date().toISOString().split('T')[0].replace(/-/g, '/'); (2认同)

Joã*_*ira 30

为什么不使用toISOString()withslice或简单的方法toLocaleDateString()

检查这里:

const d = new Date() // today, now

console.log(d.toISOString().slice(0, 10)) // YYYY-MM-DD

console.log(d.toLocaleDateString('en-US')) // M/D/YYYY
console.log(d.toLocaleDateString('de-DE')) // D.M.YYYY
console.log(d.toLocaleDateString('pt-PT')) // DD/MM/YYYY
Run Code Online (Sandbox Code Playgroud)


fms*_*msf 24

我建议你使用Moment.js http://momentjs.com/

然后你可以这样做:

moment(new Date()).format("YYYY/MM/DD");
Run Code Online (Sandbox Code Playgroud)

注意:new Date()如果你想要当前的TimeDate,你实际上不需要添加,我只是将它添加为你可以将日期对象传递给它的引用.对于当前的TimeDate,这也有效:

moment().format("YYYY/MM/DD");
Run Code Online (Sandbox Code Playgroud)

  • 如果您希望添加依赖项,那么这个答案很好 - 如果是的话,截至 2019 年,day.js 是 moment.js 的更轻量级替代品,可以作为考虑的选项 - https://github.com/iamkun/dayjs。还提到了 Luxon 和 date-fns。 (2认同)

ald*_*ani 21

var date = new Date().toLocaleDateString()
"12/30/2009"
Run Code Online (Sandbox Code Playgroud)

  • 为了比较两个日期,这个答案是最重要的。谢谢! (2认同)

mik*_*iku 15

很好的格式加载项:http://blog.stevenlevithan.com/archives/date-time-format.

你可以这样写:

var now = new Date();
now.format("yyyy/mm/dd");
Run Code Online (Sandbox Code Playgroud)


Has*_*len 15

信息

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

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);
Run Code Online (Sandbox Code Playgroud)

产量

2016年10月6日

小提琴

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

信用

来自此答案的更多信息和信用

更多

要了解更多关于.slice尝试一下编辑在W3School帮助我更好地了解如何使用它.


Jua*_*dez 15

2021 答案

您可以使用.toLocaleDateString()支持多种有用参数的本机函数,例如语言环境(选择 MM/DD/YYYY 或 YYYY/MM/DD等格式)、时区(转换日期)和格式详细信息选项(例如:1 vs 01 vs一月)。

例子

new Date().toLocaleDateString() // 8/19/2020

new Date().toLocaleDateString('en-US', {year: 'numeric', month: '2-digit', day: '2-digit'}); // 08/19/2020 (month and day with two digits)

new Date().toLocaleDateString('en-ZA'); // 2020/08/19 (year/month/day) notice the different locale

new Date().toLocaleDateString('en-CA'); // 2020-08-19 (year-month-day) notice the different locale

new Date().toLocaleString("en-US", {timeZone: "America/New_York"}); // 8/19/2020, 9:29:51 AM. (date and time in a specific timezone)

new Date().toLocaleString("en-US", {hour: '2-digit', hour12: false, timeZone: "America/New_York"});  // 09 (just the hour)
Run Code Online (Sandbox Code Playgroud)

请注意,有时要以您想要的特定格式输出日期,您必须找到与该格式兼容的语言环境。您可以在此处找到语言环境示例:https : //www.w3schools.com/jsref/tryit.asp?filename=tryjsref_tolocalestring_date_all

请注意,语言环境只是更改格式,如果要将特定日期转换为特定国家或城市的等效时间,则需要使用时区参数。


Ase*_*tam 12

使用Date get方法.

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();
Run Code Online (Sandbox Code Playgroud)

  • JavaScript Date对象的月数为零索引.一定要加1,否则12月到11月; 十一月到十月等等 (4认同)

Phi*_*Enc 9

欧洲(英语/西班牙语)格式
我需要得到当天,你可以使用这个.

function getFormattedDate(today) 
{
    var week = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
    var day  = week[today.getDay()];
    var dd   = today.getDate();
    var mm   = today.getMonth()+1; //January is 0!
    var yyyy = today.getFullYear();
    var hour = today.getHours();
    var minu = today.getMinutes();

    if(dd<10)  { dd='0'+dd } 
    if(mm<10)  { mm='0'+mm } 
    if(minu<10){ minu='0'+minu } 

    return day+' - '+dd+'/'+mm+'/'+yyyy+' '+hour+':'+minu;
}

var date = new Date();
var text = getFormattedDate(date);
Run Code Online (Sandbox Code Playgroud)


*对于西班牙语格式,只需翻译WEEK变量即可.

var week = new Array('Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado');
Run Code Online (Sandbox Code Playgroud)


输出:星期一 - 16/11/2015 14:24


Sri*_*hna 7

let dateObj = new Date();

let myDate = (dateObj.getUTCFullYear()) + "/" + (dateObj.getMonth() + 1)+ "/" + (dateObj.getUTCDate());
Run Code Online (Sandbox Code Playgroud)

作为参考,您可以查看以下详细信息

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)
Run Code Online (Sandbox Code Playgroud)

let dateObj = new Date();

let myDate = (dateObj.getUTCFullYear()) + "/" + (dateObj.getMonth() + 1)+ "/" + (dateObj.getUTCDate());
Run Code Online (Sandbox Code Playgroud)

// 返回分钟 (0-59) new Date().getMonth() // 返回月份 (0-11) new Date().getSeconds() // 返回秒 (0-59) new Date() .getTime() // 返回时间(自 1970 年 1 月 1 日以来的毫秒数)


Bas*_*asj 6

有了接受的答案,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();
Run Code Online (Sandbox Code Playgroud)