admin管理员组文章数量:1134232
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
).
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.
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
).
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.
Share Improve this question edited Dec 4, 2012 at 16:27 Wouter J 41.9k15 gold badges112 silver badges114 bronze badges asked Dec 4, 2012 at 16:25 cbm64cbm64 1,1192 gold badges12 silver badges27 bronze badges 5 |14 Answers
Reset to default 68var someDate = new Date(dateString);
someDate = someDate.getTime();
You can use Date.parse(date)
.
function epoch (date) {
return Date.parse(date)
}
const dateToday = new Date() // Mon Jun 08 2020 16:47:55 GMT+0800 (China Standard Time)
const timestamp = epoch(dateToday)
console.log(timestamp) // => 1591606075000
JavaScript dates are 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 .getTime
method.
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
:
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 ms
for your example date.
You can use the momentjs library to do this rather easily.
var epoch = moment(str).unix();
http://momentjs.com/
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()
Number(new Date('04/12/2012 07:00'))
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.
Use below site to test your time :-
https://www.epochconverter.com/
Hope it will help you :)
Since your date format is dd/MM/yyyy hh:mm, you will need to change the date format (Step 1). Else you can go to (Step 2).
Step 1. Change Date Format to MM/dd/yyyy hh:mm
var currentTime = "21/02/2021 12:00"
var monthPosition = currentTime.split('/', 2).join('/').length
var day = currentTime.substring(0, currentTime.indexOf('/'))
var month = currentTime.substring(currentTime.indexOf('/') + 1, monthPosition);
var time = month + "/" + day + currentTime.substring(month.length + day.length + 1); //"02/21/2021 12:00"
Step 2: Change Date to Epoch Time
var epochTime = Date.parse(time);
//or get current time of epoch time
var currentEpoch = Date.parse(new Date());
Note: If you trying to do Date.parse() with new Date(), and got an error of parsing date string, add toString()
var currentEpoch = Date.parse(new Date().toString());
My answer is to convert current time to epoch time using JavaScript
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.
I found this line from jslint code.
https://github.com/jslint-org/jslint/blob/a286a254f3312108cf64553914b99a642af0ac42/asset_codemirror_rollup.js#L268
+new Date
It's pretty obvious that this is Douglas Crockford way.
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')))
Single Line Code Returns in seconds
let Epoch= Date.parse(date + '') / 1000 //date in Date() Format
This is how I would do it
Math.trunc(new Date(date).getTime()/1000)
result
1671878316
const myDate = new Date("2023-04-15"); // create a new Date object with a specified date
const epochTime = myDate.getTime() / 1000; // divide by 1000 to convert milliseconds to seconds
console.log(epochTime); // output: 1687974000 (the epoch time for the specified date)
In this example, we first create a new Date object with a specified date using the new Date() constructor. We then call the getTime() method on the myDate object to get the time in milliseconds since the Unix epoch (January 1, 1970). Finally, we divide the result by 1000 to convert it to seconds, which is the format commonly used for epoch time.
本文标签: datetimeJavascript Convert Date Time string to EpochStack Overflow
版权声明:本文标题:datetime - Javascript Convert Date Time string to Epoch - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736842755a1955172.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
new Date('04/12/2012 07:00')
? – gen_Eric Commented Dec 4, 2012 at 16:30new Date('12/04/2012 07:00')
. ...? – John Commented Oct 15, 2019 at 21:37