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)
或者您可以设置新日期并提供上述值
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).
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)
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/YYYYRun 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)
ald*_*ani 21
var date = new Date().toLocaleDateString()
"12/30/2009"
Run Code Online (Sandbox Code Playgroud)
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)
欧洲(英语/西班牙语)格式
我需要得到当天,你可以使用这个.
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
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 日以来的毫秒数)
有了接受的答案,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)