将dd-mm-yy转换为javascript日期对象 [英] Convert dd-mm-yy to javascript date object

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

问题描述

我得到一个以这种格式出现的日期: ddmmyy ,我需要对它进行一些验证。
如何将其解析为javascript日期对象?

I get a date which comes in this format: ddmmyy, and I need to do some validation with it. How can I parse it to a javascript date object?

我试图搜索,上有很多内容ddmmyyyy 格式,但不是我得到的格式。

I've tried to search, and there are a lot on the ddmmyyyy format, but not the format I get.

编辑:示例日期: 031290 = 3.12.1990。

Example date: 031290 = 3.12.1990.

推荐答案

好的,这就是我如何解决它。

Ok, so here is how I solved it.

这里的答案是正确的,除非我对当前世纪的检测方式不满意;所以我基本上这样做了:

The answers here is right, except I wasn't happy with the way the detection of the current century worked; so I basically did this:

var date = new Date(year + 2000, month - 1, day);

if (date > new Date())
    date.setYear(year + 1900);

使用此代码,我可以最大限度地验证100岁,但这应该不是问题。

With this code, I can maximum validate an age of 100, but that shouldn't be a problem.

Mukul Goel在下面的评论中指出它只能验证未来的日期。可能是对的,我自己没有检查过。

Mukul Goel in the comment below points out it can only validate dates in the future. Probably right, I haven't checked it myself.

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

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