我想将日期转换为时间戳,我的输入是26-02-2012。我使用
new Date(myDate).getTime();
上面写着NaN..有人能告诉我怎么转换吗?
我想将日期转换为时间戳,我的输入是26-02-2012。我使用
new Date(myDate).getTime();
上面写着NaN..有人能告诉我怎么转换吗?
您的字符串不是Date对象指定要处理的格式。您必须自己解析它,使用像MomentJS这样的日期解析库或更老的DateJS(据我所知,目前还没有维护),或者在要求date解析它之前将其调整为正确的格式(例如,2012-02-29)。
为什么你得到NaN:当你要求new Date(…)处理一个无效的字符串时,它返回一个Date对象,该对象被设置为无效的日期(new Date("29-02-2012"). tostring()返回" invalid Date ")。在此状态下对日期对象调用getTime()将返回NaN。
你只需要倒转你的日期数字,然后用,
new Date(2012,01,26).getTime(); // 02 becomes 01 because getMonth() method returns the month (from 0 to 11)
在你的情况下:
var myDate="26-02-2012";
myDate=myDate.split("-");
new Date(parseInt(myDate[2], 10), parseInt(myDate[1], 10) - 1 , parseInt(myDate[0]), 10).getTime();
附注:英国地区在这里并不重要。
将字符串拆分为多个部分,并将它们直接提供给Date构造函数:
更新:
这是计划 捐献 var新约会=新日期(单身约会,2—1—1,单身约会[0]); console.log (newDate.getTime ());
function getTimeStamp() {
var now = new Date();
return ((now.getMonth() + 1) + '/' + (now.getDate()) + '/' + now.getFullYear() + " " + now.getHours() + ':'
+ ((now.getMinutes() < 10) ? ("0" + now.getMinutes()) : (now.getMinutes())) + ':' + ((now.getSeconds() < 10) ? ("0" + now
.getSeconds()) : (now.getSeconds())));
}
试试这个函数,它使用Date.parse()方法,不需要任何自定义逻辑:
function toTimestamp(strDate){
var datum = Date.parse(strDate);
return datum/1000;
}
alert(toTimestamp('02/13/2009 23:31:30'));
/**
* Date to timestamp
* @param string template
* @param string date
* @return string
* @example datetotime("d-m-Y", "26-02-2012") return 1330207200000
*/
function datetotime(template, date){
date = date.split( template[1] );
template = template.split( template[1] );
date = date[ template.indexOf('m') ]
+ "/" + date[ template.indexOf('d') ]
+ "/" + date[ template.indexOf('Y') ];
return (new Date(date).getTime());
}
这个重构的代码就可以做到这一点
let toTimestamp = strDate => Date.parse(strDate)
这适用于除ie8-以外的所有现代浏览器
这里有两个问题。 首先,您只能在日期的实例上调用getTime。您需要将new Date括在括号中或将其赋值给变量。
其次,您需要以适当的格式传递一个字符串。
工作的例子:
(new Date("2012-02-26")).getTime();
为了将(ISO)日期转换为Unix时间戳,我最终得到了一个比所需时间长3个字符的时间戳,所以我的年份大约是50k…
我要除以1000 new Date('2012-02-26').getTime() / 1000
其他开发人员已经提供了答案,但在我自己的方式中,你可以在不创建任何用户定义函数的情况下执行此操作,如下所示:
var timestamp = Date.parse("26-02-2012".split('-').reverse().join('-'));
alert(timestamp); // returns 1330214400000
对于那些希望具有yyyymmddHHMMSS格式的可读时间戳的人
> (new Date()).toISOString().replace(/[^\d]/g,'') // "20190220044724404"
> (new Date()).toISOString().replace(/[^\d]/g,'').slice(0, -3) // "20190220044724"
> (new Date()).toISOString().replace(/[^\d]/g,'').slice(0, -9) // "20190220"
使用示例:备份文件扩展名。/我的/ / my.file.js.20190220路径
更新:如果你来这里寻找当前的时间戳
Date.now(); //as suggested by Wilt
or
var date = new Date();
var timestamp = date.getTime();
或者简单地
new Date().getTime();
/* console.log(new Date().getTime()); */
下面的代码将把当前日期转换为时间戳。
var currentTimeStamp = Date.parse(new Date());
console.log(currentTimeStamp);
简单地在Date对象上执行一些算术运算将返回时间戳作为数字。这对于简洁表示法很有用。我发现这是最容易记住的方法,因为该方法也适用于将转换为字符串类型的数字转换回数字类型。
let d = new Date(); Console.log (d, d * 1);
它应该是这个标准日期格式YYYY-MM-DD,以使用下面的等式。例如:2020-04-24 16:51:56或2020-04-24T16:51:56+05:30。它将工作良好,但日期格式应该像这样的YYYY-MM-DD。
var myDate = "2020-04-24";
var timestamp = +new Date(myDate)
第一个答案是好的,但是使用react typescript会报错,因为split(") 对我来说,更好的方法是。
parseInt((new Date("2021-07-22").getTime() / 1000).toFixed(0))
很乐意帮忙。
如果你还需要增加时间,这个方法也能奏效 new Date('2021-07-22 07:47:05.842442+00').getTime()
这在没有时间的情况下也同样有效 new Date('2021-07-22 07:47:05.842442+00').getTime()
这也可以工作,但它不会接受时间 新日期(2021/07/22).getTime ()
最后,如果所有的都不工作,使用这个 日期(年,月,日,时,分,秒,毫秒)
注意,对于Month,计数从0开始,因此Jan === 0, Dec === 11
在某些情况下,某些日期似乎是顽固的,也就是说,即使使用日期格式,如“2022-06-29 15:16:21”,您仍然会得到null或NaN。我必须通过在空白区域中加入一个“T”来解决我的问题,即:
const inputDate = "2022-06-29 15:16:21";
const newInputDate = inputDate.replace(" ", "T");
const timeStamp = new Date(newInputDate).getTime();
这对我来说很有效!干杯!
只是提醒一下
Date.parse(“2022 - 08 - 04 - t04:02:10.909z”)
1659585730909
日期。解析(新日期(“2022 - 08 - 04 - t04:02:10.909z”))
1659585730000