显示日期到月份dd,yyyy [英] Displaying date to month dd, yyyy

查看:34
本文介绍了显示日期到月份dd,yyyy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于time_cancel = 2019-01-06T23:29:35.000Z,想将其格式化为dd yyyy(在本例中为2019年1月6日)

given time_cancel = 2019-01-06T23:29:35.000Z, would like to format it to month dd, yyyy (in this example, it is January 06, 2019)

在以下代码中,我必须编写12 if else块才能将mm转换为月也就是说,在这种情况下,是将01转换为1月.

In the following code, I have to write 12 if else block to convert mm to month i.e. in this case, is to convert 01 to January.

有没有更好/更快的方法来将time_cancel转换为Javascript或Typescript/es6/react中的月份dd,yyyy格式.预先感谢!

is there a better /faster way to convert time_cancel to format month dd, yyyy in Javascript or Typescript/es6/react. Thanks in advance!

let date = time_cancel.split("T")[0];
var dateArray = date.split("-"); 
var month;

// 12 if else code
if(dateArray[1]=='01') month='January';
else if(dateArray[1]=='02') month='February';
 ....
else if (dateArray[1]=='12') month='December;

var res=month+" "+dateArray[2]+", "+dateArray[0];

推荐答案

.toLocaleDateString()可以给您DD月YYYY,但是它不允许您自定义订单.我很想坚持使用这种默认行为,但是如果您确实需要的话,可以将其切碎.您仍然可以从区域设置意识和翻译中受益...

.toLocaleDateString() can give you DD Month YYYY, but it doesn't let you customise the order. I would be very tempted to stick with this default behaviour, but you can chop it up if your really must. You still profit from the locale awareness and translations...

const formattedDate = new Date("2019-01-06T23:29:35.000Z")
.toLocaleDateString({},
  {timeZone:"UTC",month:"long", day:"2-digit", year:"numeric"}
  )
  console.log(formattedDate)
const sp = formattedDate.split(' ')
console.log(`${sp[1]} ${sp[0]}, ${sp[2]}`)

这篇关于显示日期到月份dd,yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆