Javascript 将日期时间字符串转换为纪元
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13707333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript Convert Date Time string to Epoch
提问by cbm64
so I give up...been trying to do this all day;
所以我放弃了……一整天都在尝试这样做;
I have a string that supplies a date and time in the format dd/MM/yyyy hh:mm(04/12/2012 07:00).
我有一个字符串,它以dd/MM/yyyy hh:mm( 04/12/2012 07:00)格式提供日期和时间。
I need to turn that into an Epoch date so I can do some calculations upon it. I cannot modify the format in which the date time is sent to me.
我需要把它变成一个纪元日期,以便我可以对它进行一些计算。我无法修改发送给我的日期时间的格式。
JavaScript or jQuery is fine.
JavaScript 或 jQuery 都可以。
采纳答案by Bergi
JavaScript datesare internally stored as milliseconds since epoch. You just need to convert it to a number, e.g. with the unary +operator, to get them. Or you can use the .getTimemethod.
JavaScript 日期在内部存储为自纪元以来的毫秒数。您只需要将其转换为数字,例如使用一元运算+符,即可获得它们。或者您可以使用该.getTime方法。
The harder will be parsing your date string. You likely will use a regex to extract the values from your string and pass them into Date.UTC:
解析您的日期字符串将更加困难。您可能会使用正则表达式从字符串中提取值并将它们传递到Date.UTC:
var parts = datestring.match(/(\d{2})\/(\d{2})\/(\d{4}) (\d{2}):(\d{2})/);
return Date.UTC(+parts[3], parts[2]-1, +parts[1], +parts[4], +parts[5]);
This will yield 1354604400000 msfor your example date.
这将产生1354604400000 ms您的示例日期。
回答by Crayon Violent
var someDate = new Date(dateString);
someDate = someDate.getTime();
回答by chovy
You can use the momentjs library to do this rather easily.
您可以使用 momentjs 库轻松完成此操作。
var epoch = moment(str).unix();
回答by yussan
i used this code to convert my string datetime to epoch
我使用此代码将我的字符串日期时间转换为纪元
new Date(<datetime string>').getTime()
for example :
例如 :
let epoch = new Date('2016-10-11').getTime()
回答by Shubham Jain
var time = new Date().getTime() / 1000 + 900 + 330*60;
console.log("time = "+time);
getTime() will return current time with milleseconds in last 3 digit so divide it by 1000 first. Now I have added 900 means 15 min which I need from my current time(You can delete if you do not require further delay time), 330*60(5 hr 30) is required to convert GMT time to IST which is my current region time.
getTime() 将在最后 3 位以毫秒返回当前时间,因此先将其除以 1000。现在我添加了 900 表示我需要从当前时间开始的 15 分钟(如果不需要进一步的延迟时间,可以删除),需要 330*60(5 hr 30) 将 GMT 时间转换为 IST,这是我当前的区域时间。
Use below site to test your time :-
使用以下网站测试您的时间:-
https://www.epochconverter.com/
https://www.epochconverter.com/
Hope it will help you :)
希望它会帮助你:)
回答by Vivek Mehta
My answer is to convert current time to epoch time using JavaScript
我的答案是使用 JavaScript 将当前时间转换为纪元时间
const currentDate= Math.floor(new Date() / 1000);
const currentDate= Math.floor(new Date() / 1000);
console.log(currentDate); //whatever value it will print you can check the same value to this website https://www.epochconverter.com/to confirm.
的console.log(的currentdate); //无论它将打印什么值,您都可以在此网站https://www.epochconverter.com/上检查相同的值以进行确认。
回答by Rohit Parte
Easiest way to do is -
最简单的方法是——
const moment = require('moment')
function getUnixTime () { return this.getTime() / 1000 | 0 }
let epochDateTime = getUnixTime(new Date(moment().add(365, 'days').format('YYYY-
MM-DD hh:mm:ss')))


