警报(dateObj)给出周三2009年12月30日00:00:00 GMT+0800
如何获得日期格式为2009/12/30?
警报(dateObj)给出周三2009年12月30日00:00:00 GMT+0800
如何获得日期格式为2009/12/30?
不错的格式化插件:http://blog.stevenlevithan.com/archives/date-time-format。
你可以这样写:
var now = new Date();
now.format("yyyy/mm/dd");
使用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 dt = new Date();
dt.getFullYear() + "/" + (dt.getMonth() + 1) + "/" + dt.getDate();
因为月份索引是以0为基础的,所以必须加1。
Edit
有关日期对象函数的完整列表,请参见
Date
getMonth()
根据当地时间返回指定日期中的月份(0-11)。
getUTCMonth()
根据世界时返回指定日期中的月份(0-11)。
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;
或者你可以设置新的日期并给出上面的值
我建议你使用Moment.js http://momentjs.com/
然后你可以这样做:
moment(new Date()).format("YYYY/MM/DD");
注意:如果你想要当前的TimeDate,你实际上不需要添加新的Date(),我只添加它作为一个引用,你可以传递一个日期对象给它。对于当前的TimeDate,这也适用:
moment().format("YYYY/MM/DD");
欧洲(英语/西班牙语)格式 如果你也想知道今天的日期,你可以用这个。
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);
*对于西班牙语格式,只需翻译WEEK变量。
var week = new Array('Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado');
输出:周一- 16/11/2015 14:24
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"
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 date = new Date().getFullYear() + "-" + new Date().getMonth() + 1 + "-" + new Date().getDate();
我使用这个工作,如果你传递它一个日期obj或js时间戳:
getHumanReadableDate: function(date) {
if (date instanceof Date) {
return date.getDate() + "/" + (date.getMonth() + 1) + "/" + date.getFullYear();
} else if (isFinite(date)) {//timestamp
var d = new Date();
d.setTime(date);
return this.getHumanReadableDate(d);
}
}
对于已接受的答案,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 日期 = 新日期(); var formattedDate = '${date.getFullYear()}/${(date.getMonth() + 1)}/${date.getDate()}'; 控制台.log(格式化日期);
ES2018引入了正则表达式捕获组,你可以用它来捕获日、月和年:
const REGEX = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})/;
const results = REGEX.exec('2018-07-12');
console.log(results.groups.year);
console.log(results.groups.month);
console.log(results.groups.day);
这种方法的优点是可以捕获非标准字符串日期格式的日、月、年。
引用https://www.freecodecamp.org/news/es9——javascripts -状态- -艺术-在- 2018 - 9 - a350643f29c/
为什么不使用方法toISOString()与切片或简单的toLocaleDateString()?
注意toISOString返回的时区总是零UTC偏移量,而在toLocaleDateString中它是用户代理的时区。
检查:
const d = new Date() //今天,现在 //时区0 UTC偏移量 console.log (d.toISOString()。slice(0, 10)) // YYYY-MM-DD //用户代理的时区 console.log(d.toLocaleDateString('en-CA')) // YYYY-MM-DD .log(d.toLocaleDateString('en-CA') console.log(d.toLocaleDateString('en-US')) // M/D/YYYY .log(d.toLocaleDateString('en-US') console.log(d.toLocaleDateString('de-DE')) // d.m.y yyyy .log console.log(d.toLocaleDateString('pt-PT')) // DD/MM/YYYY .log(d.toLocaleDateString('pt-PT')
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)
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)
注意,有时要以特定的格式输出日期,必须找到与该格式兼容的区域设置。 您可以在这里找到本地示例:https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_tolocalestring_date_all
请注意locale只是改变格式,如果你想将特定日期转换为特定国家或城市的等效时间,那么你需要使用timezone参数。
它将从用户的浏览器设置中收集语言
使用选项对象中的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()
一行,使用解构。
创建3个字符串类型的变量:
const [year, month, day] = (new Date()).toISOString().substr(0, 10).split('-')
生成3个类型为number (integer)的变量:
const [year, month, day] = (new Date()).toISOString().substr(0, 10).split('-').map(x => parseInt(x, 10))
从那时起,你就可以很容易地以任何你喜欢的方式组合它们:
const [year, month, day] = (new Date()).toISOString().substr(0, 10).split('-');
const dateFormatted = `${year}/${month}/${day}`;