jQuery:将字符串变量传递给date对象 [英] jQuery: pass string variable to date object

查看:388
本文介绍了jQuery:将字符串变量传递给date对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面包含许多变量,其中包含使用moment.js的字符串格式(yyyy-mm-dd)的日期。

I have a page with a number of variables that contain a date in string format (yyyy-mm-dd) which originates from using moment.js.

有没有办法可以将这样的变量传递给Javascript日期对象。将其转换为Javascript日期对象?
我对时间不感兴趣,只要我可以将日期转换成一个很好的日期对象。

Is there a way I can pass such a variable to a Javascript date object resp. to convert it to a Javascript date object ? I am not interested in the time so as long as I can get the date converted to a date object that would be great.

我尝试了以下操作,但这不起作用,我找不到使用moment.js的方式:

I tried the following but this doesn't work and I couldn't find a way using moment.js:

var newVar = new Date(dateVar);

非常感谢任何帮助,Tim。

Many thanks for any help with this, Tim.

推荐答案

首先我会说以下内容应该适用于你..

first of all i will say following should work for you..

var dateVar = "2010-10-30";
var d=new Date(dateVar);

如果你说上面没有工作检查下面的一个 -

if you say above not working check the below one -

var dateVar = "2010-10-30";
var dsplit = dateVar.split("-");
var d=new Date(dsplit[0],dsplit[1]-1,dsplit[2]);

为了检查jsfiddle ..两个都正常工作.. JSFiddle

for the proof check the jsfiddle.. both is working fine.. JSFiddle

这篇关于jQuery:将字符串变量传递给date对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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