在JavaScript中将日期从一种格式转换为另一种格式 [英] Convert Date from one format to another format in JavaScript
问题描述
当一种格式为dd / mm / yy,另一种格式为mm / dd / yy时,问题就会开始
格式动态变化,我的格式为字符串,但我需要一个像
Date newDate = convert(currentDate,currentFormatString,newFormatString)。
我该怎么做?
您应该查看 momentjs ,这是一个javascript日期/时间库。这样,您可以轻松地在不同格式的日期之间进行转换。在你的情况下,它将是:
string newDate = moment(currentDate,currentFormatString).format(newFormatString)
例如, moment(21/10/14,DD / MM / YY)格式(MM / DD / YY)
将返回10/21/14
I have a string of a date in javascript in format #1. I need to convert it to format #2.
The problem starts when one format is "dd/mm/yy" and the other is "mm/dd/yy".
The formats change dynamically and I have the formats as strings, but I need a function like
Date newDate = convert(currentDate, currentFormatString, newFormatString).
How can I do it?
You should look into momentjs, which is a javascript date/time library. With that, you can easily convert between dates of different format. In your case, it would be:
string newDate = moment(currentDate, currentFormatString).format(newFormatString)
For example, moment("21/10/14", "DD/MM/YY").format("MM/DD/YY")
would return "10/21/14"
这篇关于在JavaScript中将日期从一种格式转换为另一种格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!