如何将日期转换为时间戳? [英] How to convert date to timestamp?

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

问题描述

我想将日期转换为时间戳,我的输入是 26-02-2012 。我使用

I want to convert date to timestamp, my input is 26-02-2012. I used

new Date(myDate).getTime();

上面写着NaN ..有人能告诉我如何转换吗?

It says NaN.. Can any one tell how to convert this?

推荐答案

将字符串拆分为多个部分,并将其直接提供给Date构造函数:

Split the string into its parts and provide them directly to the Date constructor:

更新:

var myDate = "26-02-2012";
myDate = myDate.split("-");
var newDate = new Date( myDate[2], myDate[1] - 1, myDate[0]);
console.log(newDate.getTime());

这篇关于如何将日期转换为时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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