{ // example 1let f = new Intl.DateTimeFormat('en');let a = f.formatToParts();console.log(a);}{ // example 2let f = new Intl.DateTimeFormat('hi');let a = f.formatToParts();console.log(a);}
更好的方法是将格式数组映射到结果字符串:
function join(t, a, s) {function format(m) {let f = new Intl.DateTimeFormat('en', m);return f.format(t);}return a.map(format).join(s);}
let a = [{day: 'numeric'}, {month: 'short'}, {year: 'numeric'}];let s = join(new Date, a, '-');console.log(s);
let d = new Date(2010, 7, 5);let ye = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(d);let mo = new Intl.DateTimeFormat('en', { month: 'short' }).format(d);let da = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(d);console.log(`${da}-${mo}-${ye}`);
var today = new Date().toISOString().slice(0, 10);
请记住,上述解决方案没有不会考虑您的时区偏移量。
你可以考虑改用这个函数:
function toJSONLocal (date) {var local = new Date(date);local.setMinutes(date.getMinutes() - date.getTimezoneOffset());return local.toJSON().slice(0, 10);}
如果您在一天的开始/结束时执行此代码,这将为您提供正确的日期。
var date = new Date();
function toLocal(date) {var local = new Date(date);local.setMinutes(date.getMinutes() - date.getTimezoneOffset());return local.toJSON();}
function toJSONLocal(date) {var local = new Date(date);local.setMinutes(date.getMinutes() - date.getTimezoneOffset());return local.toJSON().slice(0, 10);}
// check out your devtools consoleconsole.log(date.toJSON());console.log(date.toISOString());console.log(toLocal(date));
console.log(toJSONLocal(date));
var d = (new Date()).toString().replace(/\S+\s(\S+)\s(\d+)\s(\d+)\s.*/,'$2-$1-$3');// date will be formatted as "14-Oct-2015" (pass any date object in place of 'new Date()')
date.toISOString().replace(/^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+).(\d+)Z$/, function (a,y,m,d) {return [d,['Jan','Feb','Mar','Apr','May','Jun','Jul','Ago','Sep','Oct','Nov','Dic'][m-1],y].join('-')})
function init(){var d = new Date();var day = d.getDate();var x = d.toDateString().substr(4, 3);var year = d.getFullYear();document.querySelector("#mydate").innerHTML = day + '-' + x + '-' + year;}window.onload = init;
function dateToYMD(date) {var d = date.getDate();var m = date.getMonth() + 1; //Month from 0 to 11var y = date.getFullYear();return '' + y + '-' + (m<=9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d);}
console.log(dateToYMD(new Date(2017,10,5))); // Nov 5
OP格式可以像这样生成:
function dateToYMD(date) {var strArray=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];var d = date.getDate();var m = strArray[date.getMonth()];var y = date.getFullYear();return '' + (d <= 9 ? '0' + d : d) + '-' + m + '-' + y;}console.log(dateToYMD(new Date(2017,10,5))); // Nov 5
new Date().toString(); // e.g. "Fri Nov 21 2016 08:00:00 GMT+0100 (W. Europe Standard Time)"
注意:可以从这些格式中生成自定义输出>
new Date().toISOString().slice(0,10); //return YYYY-MM-DD
示例片段:
console.log("1) "+ new Date().toDateString());console.log("2) "+ new Date().toISOString());console.log("3) "+ new Date().toJSON());console.log("4) "+ new Date().toLocaleDateString());console.log("5) "+ new Date().toLocaleString());console.log("6) "+ new Date().toLocaleTimeString());console.log("7) "+ new Date().toString());console.log("8) "+ new Date().toISOString().slice(0,10));
console.log("1) "+ new Date().toLocaleString('en-US'));console.log("2) "+ new Date().toLocaleString('ko-KR'));console.log("3) "+ new Date().toLocaleString('de-CH'));
console.log("4) "+ new Date().toLocaleString('en-GB', { hour12: false }));console.log("5) "+ new Date().toLocaleString('en-GB', { hour12: true }));
var date = new Date();var options = { year: 'numeric', month: 'short', day: '2-digit'};var _resultDate = new Intl.DateTimeFormat('en-GB', options).format(date);// The _resultDate is: "12 Oct 2017"// Replace all spaces with - and then log it.console.log(_resultDate.replace(/ /g,'-'));
结果是:"12-Oct-2017"
可以使用选项参数自定义日期和时间格式。
Intl.DateTimeFormat对象是启用语言敏感日期和时间格式的对象的构造函数。
语法
new Intl.DateTimeFormat([locales[, options]])Intl.DateTimeFormat.call(this[, locales[, options]])
const date = new Date('2018/07/17 12:08:56');const sdf = new SimpleDateFormat();console.log(sdf.formatWith("yyyy-MM-dd'T'HH:mm:ssXXX", date));//to be "2018-07-17T12:08:56+09:00"
但是在Internet Explorer中它有问题。由此我了解我们可以在给定日期应用一种日期格式。如果我们想要第二种日期格式,它应该应用于新日期,而不是第一个日期格式结果。并且还观察到第一次应用'MM-DD-YYY'和下一个'MM-DD-YY'在Internet Explorer中工作。为了清楚地理解,请在链接中找到我的问题:在Internet Explorer 11中使用Moment.js日期格式时出错。
const date = new Date("2010-08-10");
let d=new Intl.DateTimeFormat('en-GB',{year:"numeric", month:"short",day:"2-digit"}).format(date).split(" ").join("-");
console.log(d);
let date = new Date().toLocaleDateString('en-us',{day: 'numeric'})let month = new Date().toLocaleDateString('en-us',{month: 'long'})let year = new Date().toLocaleDateString('en-us',{year: 'numeric'})const FormattedDate = `${date}-${month}-${year}`console.log(FormattedDate) // 26-March-2022
((d,x)=>`${d.getFullYear()}-${x(d.getMonth()+1)}-${x(d.getDate())}`)(new Date(), (x)=>x.toString().padStart(2,"0"))
// today that produces// '2022-09-28'// the same as new Date().toISOString().slice(0, 10)// but provides a good framework for other orders or values
((d,x,y)=>`${x(d.getDate())} ${y(d.getMonth())} ${d.getFullYear()}`)(new Date(),(x)=>x.toString().padStart(2,"0"),(m)=>"jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec".split(',')[m])
// As of this writing, that yields...// '28 sep 2022'