[英]Changing date format using Jquery
I have date got from a API like this 2018-08-27T09:28:53, now I want to convert it to a format like August 27, 2018. 我有一个类似2018-08-27T09:28:53这样的API的日期,现在我想将其转换为2018年8月27日这样的格式。
I have tried using this 我试过使用这个
var d = new Date(val.date);
d = d.toDateString();
the above code gives a date like Mon Jul 27 2018. How can I add the comma and separate the month day and year? 上面的代码给出的日期类似于2018年7月27日。如何添加逗号并分隔月份和年份?
Is there a better way to format iso 8601 date to desired format, for me like August 27, 2018. 是否有更好的方法将iso 8601日期格式化为所需格式,例如2018年8月27日。
The easiest would be that you use options
with toLocaleDateString
最简单的方法是将
options
与toLocaleDateString
var d = new Date(val.date);
var options = { year: 'numeric', month: 'long', day: 'numeric' };
d = d.toLocaleDateString("en-US", options)
var d = new Date(); var options = { year: 'numeric', month: 'long', day: 'numeric' }; console.log( d.toLocaleDateString('en-US', options) );
The easiest way for you is to use date.js : 最简单的方法是使用date.js :
var date = new Date('2018-08-27');
var newDate = date.toString('dd-MM-yy');
Or you can Nativity: 或者您可以耶稣降生:
var dateAr = '2018-08-27'.split('-');
var newDate = dateAr[1] + '-' + dateAr[2] + '-' + dateAr[0].slice(-2);
console.log(newDate);
Pure js: 纯js:
let objDate = new Date(), month = objDate.toLocaleString('en-us', { month: 'long' }), day = objDate.getDate(), year = objDate.getFullYear() console.log(`${month} ${day}, ${year}`)
or 要么
let objDate = new Date(), date = objDate.toLocaleString('en-us', { year: 'numeric', month: 'long', day: 'numeric' }) console.log(date)
You can use some plugin for that, like momentjs , or use following code: 您可以为此使用一些插件,例如momentjs ,或使用以下代码:
function formatDate(date) {
var months = [
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"
];
var d = date.getDate(),
m = date.getMonth(),
y = date.getFullYear();
return months[m] + ', ' + d + ' ' + y;
}
console.log(formatDate(new Date()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.