在javascript中将字符串转换为Date [英] Convert string to Date in javascript

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

问题描述



我在变量中读取日历控件的日期。日期可以采用dd-mm-yyyy或dd / mm / yyyy或mm-dd-yyyy或mm / dd / yyyy格式。

然后在另一个变量中我得到今天的日期。

如何将字符串变量中的日期值转换为mm / dd / yyyy格式,以便我可以比较两个日期?

我想检查哪个日期更大。



这是我的代码

  var  date = objParam [ 0 ]。value; 
var todaysDate = new 日期();
todaysDate = todaysDate.getMonth()+ 1 + ' /' + todaysDate.getDate()+ ' /' + todaysDate。和getFullYear();

if (date> todaysDate){
alert( 请选择有效日期);
}





任何帮助表示赞赏。



谢谢,

Lok

解决方案

看看: http: //momentjs.com/docs/ [ ^ ]



这个对你有用: http:// momentjs。 com / docs /#/ displays / difference / [ ^ ]



它提供了广泛的功能来处理日期。请记住,日期没有格式。这是Date的字符串表示。



问候..


以下行的意义是什么?:



 todaysDate = todaysDate.getMonth()+  1  + '  /' + todaysDate.getDate()+ '  /' + todaysDate.getFullYear(); 







我在上一个项目中这样做了(我必须控制令牌是否过期):



  var  today =  new   Date (); 
if (compareTime& gt; today.getTime()){
...
}


Hi,
I read a date from Calendar control in a variable. The date may be in format dd-mm-yyyy or dd/mm/yyyy or mm-dd-yyyy or mm/dd/yyyy.
Then in another variable I get today's date.
How to convert the date value in string variable to mm/dd/yyyy format so that I can compare the two dates?
I want to check which date is bigger.

Here is my code

var date = objParam[0].value;
var todaysDate = new Date();
todaysDate = todaysDate.getMonth() + 1 + '/' + todaysDate.getDate() + '/' + todaysDate.getFullYear();

if (date > todaysDate) {
    alert("Please select the valid date");
}



Any help appreciated.

Thanks,
Lok

解决方案

Have a look at : http://momentjs.com/docs/[^]

This one will be useful for you : http://momentjs.com/docs/#/displaying/difference/[^]

It provides a wide range of functions to handle the Date. Remember, date doesn't have format. It is Date's string representation.

Regards..


Whats the sens of the following line?:

todaysDate = todaysDate.getMonth() + 1 + '/' + todaysDate.getDate() + '/' + todaysDate.getFullYear();




I did in my last project like this (I had to control if a token is expired):

var today = new Date();
if (compareTime > today.getTime()) {
...
}


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

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