JavaScript重新格式化日期字符串 [英] Javascript reformat date string

查看:44
本文介绍了JavaScript重新格式化日期字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个日期:

Mon Feb 02 2015 05:18:44 GMT+0000 (UTC) 

如何将其重新格式化为更友好的内容,例如 2/2/2015 我正在使用javascript.

How can I reformat it to something more friendlier such as 2/2/2015 I am using javascript.

我尝试使用 .format dateFormat ,但是它们都返回未定义的值.

I tried using .format and dateFormat but they both return undefined value.

我该怎么做?我不想使用任何正则表达式.

How can I do this? I don't want to use any regex please.

推荐答案

如果您想要最简单的方法,只需合并所有单独的部分

If you want the simplest way just concat all of the separate parts

var output = dt.getMonth( ) + 1 +'/'+ dt.getDate( ) + '/' +dt.getFullYear( );

有一些库可以在需要时处理更高级的内容,但这是我能想到的最简单的方法.

There are a few libraries that will handle more advanced stuff if you need it, but that is the lightest way I can think of doing what you are asking.

这篇关于JavaScript重新格式化日期字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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