使用javascript将数字转换为日期 [英] Convert number into date using javascript

查看:127
本文介绍了使用javascript将数字转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的日期在文本字段中不带"/",并且为 mm/dd/yy 格式.我们收到的格式为03292014.我想从该数字中获取月,日和年,例如2014年3月29日

I have date in without "/" in text field and it is in mm/dd/yy format. We received input in this format 03292014. I want to get month,date and year from this number like 03/29/2014

var m = new Date(3292014*1000)

console.log(m.toGMTString())

推荐答案

您可以这样做:

var m = '03292014'.match(/(\d\d)(\d\d)(\d\d\d\d)/);
var d = new Date(m[3], m[1] - 1, m[2]);

或将输入转换为标准的"YYYY-MM-DD"格式:

Or convert the input into a standard "YYYY-MM-DD" format :

var d = new Date('03292014'.replace(
    /(\d\d)(\d\d)(\d\d\d\d)/, '$3-$1-$2'
));

规格: http://es5.github.io/#x15.9.1.15.

根据 Xotic750的评论,以防万一您只想更改格式:

According to Xotic750's comment, in case you just want to change the format :

var input = '03292014';
input = input.replace(
    /(\d\d)(\d\d)\d\d(\d\d)/, '$1/$2/$3'
);
input; // "03/29/14"

这篇关于使用javascript将数字转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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