将日期转换为mj,Y?在Javascript中
当前我有一个名为将日期转换为mj,Y?在Javascript中,javascript,Javascript,当前我有一个名为uniqueDate 如果我试图获取这个变量的值console.log(uniqueDate) 这将为我提供一个这样的登录检查元素 [“2020-05-24”、“2020-05-25”、“2020-05-26”、“2020-05-27”、“2020-05-28”、“2020-05-29”] 在php中有date('mj,Y',strotime()) 这将导致类似这样的结果 2020年5月24日 2020年5月25日 2020年5月26日 2020年5月27日 2020年5月28日
uniqueDate
如果我试图获取这个变量的值console.log(uniqueDate)代码>
这将为我提供一个这样的登录检查元素
[“2020-05-24”、“2020-05-25”、“2020-05-26”、“2020-05-27”、“2020-05-28”、“2020-05-29”]
在php中有date('mj,Y',strotime())
这将导致类似这样的结果
2020年5月24日
2020年5月25日
2020年5月26日
2020年5月27日
2020年5月28日
2020年5月29日
我想将varuniqueDate
的日期格式转换为mj,Y
,就像上面的示例日期一样
使用Jquery?或者只是JS
var dates = ["2020-05-24", "2020-05-25", "2020-05-26", "2020-05-27", "2020-05-28", "2020-05-29"]
var data = [];
for (var date of dates)
{
date = new Date(date);
var final = date.toDateString();
var final = final.substring(4, 15);
data.push(final);
}
console.log(data);
这行代码'var final=final.substring(4,15);'结果如下
[ 'May 24 2020',
'May 25 2020',
'May 26 2020',
'May 27 2020',
'May 28 2020',
'May 29 2020' ]
[ 'Sun May 24 2020',
'Mon May 25 2020',
'Tue May 26 2020',
'Wed May 27 2020',
'Thu May 28 2020',
'Fri May 29 2020' ]
没有这行代码'var final=final.substring(4,15);'具体如下
[ 'May 24 2020',
'May 25 2020',
'May 26 2020',
'May 27 2020',
'May 28 2020',
'May 29 2020' ]
[ 'Sun May 24 2020',
'Mon May 25 2020',
'Tue May 26 2020',
'Wed May 27 2020',
'Thu May 28 2020',
'Fri May 29 2020' ]
这很简单。只需创建一个新的Javascript日期对象并从该对象调用toDateString()方法。如果不需要星期几,只需调用substring方法
String.prototype.insert=函数(索引,str){
返回此.slice(0,索引)+str+this.slice(索引)
}
var a=新日期('2020-05-27');
log(a.toDateString());
log(a.toDateString().substring(4));
log(a.toDateString().substring(4.insert(-5',,'))代码>如果你经常处理日期,我是moment.js的忠实粉丝,但对于简单的格式,这是你最接近PHP的方式:
let uniqueDate = [ // better to call it uniqueDates
"2020-05-24",
"2020-05-25",
"2020-05-26",
"2020-05-27",
"2020-05-28",
"2020-05-29"
]
let formatedDates = uniqueDate.map((arr) => {
let date = new Date(arr)
var options = { month: "short", day: "numeric", year: "numeric" }
return new Intl.DateTimeFormat("en-US", options).format(date)
})
console.log(formatedDates)
// ["May 23, 2020", "May 24, 2020", "May 25, 2020", "May 26, 2020", "May 27, 2020", "May 28, 2020"]
这里是小提琴:纯JS代码,
-只需使用名为convertDate的函数
const convertDate=dx=>(新日期(dx)).toLocaleDateString('en-US',{月:'short',日:'2位',年:'numeric'});
对于(让dt为[“2020-05-24”、“2020-05-25”、“2020-05-26”、“2020-05-27”、“2020-05-28”、“2020-05-29”])
{
文件。写入(日期(dt),“
”)
}
您可以尝试以下方法:
var日期=新日期('2020-05-24');
var normalizedDate=新日期(Date.getTime()-Date.getTimezoneOffset()*-60000);
var formattedDate=normalizedDate.toLocaleDateString('en-US'{
日期:'数字',
月:“短”,
年份:“数字”
});
console.log(格式化日期)代码>我通过创建months
对象来实现这一点。我们甚至可以定制它,当它涉及到不同的月份比英语月
const list=[“2020-05-24”、“2020-05-25”、“2020-05-26”、“2020-05-27”、“2020-05-28”、“2020-05-29”];
const monthObj={
“01”:“一月”,
“02”:“二月”,
“03”:“三月”,
“04”:“四月”,
“05”:“五月”,
“06”:“六月”,
“07”:“7月”,
“08”:“八月”,
“09”:“9月”,
“10”:“10月”,
“11”:“11月”,
“12”:“12月”,
}
const formattedList=list.map((项目、索引)=>{
const arr=项目拆分('-');
施工年份=arr[0];
施工月=arr[1];
const day=arr[2];
返回monthObj[月]+“”+日+
“,”+年;
})
log(formattedList)
使用像moment.js或date这样的日期库-fns@MisterJojo对逗号的应答所做的更改