在JavaScript中将日期从一种格式转换为另一种格式 [英] Convert Date from one format to another format in JavaScript

查看:103
本文介绍了在JavaScript中将日期从一种格式转换为另一种格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串的日期在JavaScript格式#1。我需要将其转换为格式#2。



当一种格式为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屋!

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