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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2013255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:23:27  来源:igfitidea点击:

How to get year/month/day from a date object?

javascriptdate

提问by user198729

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

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

How to get date in format 2009/12/30?

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

回答by Hiyasat

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;

or you can set new date and give the above values

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

回答by rahul

var dt = new Date();

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

Since month index are 0 based you have to increment it by 1.

由于月份索引是基于 0 的,因此您必须将其增加 1。

Edit

编辑

For a complete list of date object functions see

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

Date

日期

getMonth()

Returns the month (0-11) in the specified date according to local time.

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

getUTCMonth()

Returns the month (0-11) in the specified date according to universal time.

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

回答by davidcondrey

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"

回答by fmsf

I would suggest you to use Moment.js http://momentjs.com/

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

Then you can do:

然后你可以这样做:

moment(new Date()).format("YYYY/MM/DD");

Note: you don't actualy need to add new Date()if you want the current TimeDate, I only added it as a reference that you can pass a date object to it. for the current TimeDate this also works:

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

moment().format("YYYY/MM/DD");

回答by Hastig Zusammenstellen

info

信息

If a 2 digit month and date is desired (2016/01/01 vs 2016/1/1)

如果需要 2 位数的月份和日期(2016/01/01 与 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);

output

输出

2016/10/06

2016/10/06

fiddle

小提琴

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

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

credit

信用

More info from and credit to this answer

来自此答案的更多信息并归功于此答案

more

更多的

To learn more about .slicethe try it yourself editorat w3schools helped me understand better how to use it.

要详细了解w3schools.slicetry it own 编辑器,我帮助我更好地了解了如何使用它。

回答by miku

Nice formatting add-in: http://blog.stevenlevithan.com/archives/date-time-format.

不错的格式化插件:http: //blog.stevenlevithan.com/archives/date-time-format

With that you could write:

有了它,你可以写:

var now = new Date();
now.format("yyyy/mm/dd");

回答by Aseem Gautam

Use the Date get methods.

使用日期获取方法。

http://www.tizag.com/javascriptT/javascriptdate.php

http://www.tizag.com/javascriptT/javascriptdate.php

http://www.htmlgoodies.com/beyond/javascript/article.php/3470841

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

回答by aldokkani

var date = new Date().toLocaleDateString()
"12/30/2009"

回答by Philip Enc

EUROPE (ENGLISH/SPANISH) FORMAT
I you need to get the current day too, you can use this one.

欧洲(英语/西班牙语)格式
我也需要获取当前日期,您可以使用此格式

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


*For Spanish format, just translate the WEEK variable.


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

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


Output: Monday - 16/11/2015 14:24


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

回答by Basj

With the accepted answer, January 1st would be displayed like this: 2017/1/1.

随着接受的答案,1月1日将显示如下:2017/1/1

If you prefer 2017/01/01, you can use:

如果您愿意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();