I'm trying to convert string into epoch time in milliseconds using specs found on:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
For some reason the following snippet of codes returns March 13 instead of Feb 24, 2014.
Snippet 1:
alert(Date(Date.parse("2014-02-24T09:49:22.000-0800")));
Output: Thu Mar 13 2014 21:51:41 GMT-0700 (Pacific Daylight Time)
Snippet 2:
alert(Date(Date.parse("2014-02-24")));
Output: Thu Mar 13 2014 21:51:41 GMT-0700 (Pacific Daylight Time)
Is this some sort of timezone issue or what is the mistake that i have done ?
try new
alert(new Date(Date.parse("2014-02-24")))
Try this:
function parseDate(input) {
var parts = input.split('-');
return new Date(parts[0], parts[1]-1, parts[2]); // Note: months are 0-based
}
or
console.log(new Date(Date.parse("2014-02-08T00:00:00Z")).toString());
Related
I have a Date format like this "Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time)"
I want to convert that above format to this format 2020-04-20T00:00:00.000Z
Actually I tried this JSON.stringify(new Date("Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time)")) while using this am getting the output one day before 2020-04-19T18:30:00.000Z
so please anyone help me to convert this date format "Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time)" like this 2020-04-20T00:00:00.000Z
Thanks in Advance.
Your date seems to be a standard string representation of new Date(), you can get the desired format by using new Date().toISOString()
console.log(new Date().toString())
console.log(new Date().toISOString())
// To create it from string
const dateStr = "Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time)"
console.log(new Date(dateStr).toISOString())
Anurag Srivastava's answer shows how you should parse the string and format it in the required format (given that the string is in one of the two formats supported by ECMA-262 and considering Why does Date.parse give incorrect results?).
Note that "Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time)" is the same instant in time as "2020-04-19T18:30:00.000Z". The first string is offset from UTC by 5 hr 30 min, so the equivalent UTC time is 5 hr 30 min earlier, which means the date is the previous day.
You haven't given a reason why you want to treat it as UTC and not consider the offset, so I don't think you should.
However, if you do have a good reason to parse it as UTC and ignore the supplied offset, then you can either:
Modify the input string to set the offset as +0 and parse it using the built–in parser
Parse the string yourself and treat it as UTC
let s = "Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time)";
// #1 Modify the input string, setting the offset to +0
let d = new Date(s.replace(/GMT.*$/,'GMT+0000')).toISOString();
console.log(d.toISOString());
// #2 Bespoke parser
function parseAsUTC(s) {
let months = ['jan','feb','mar','apr','may','jun',
'jul','aug','sep','oct','nov','dec'];
let b = s.split(/\W/);
return new Date(Date.UTC(b[3], months.indexOf(b[1].toLowerCase()),
b[2], b[4], b[5], b[6]));
}
console.log(parseAsUTC(s).toISOString());
I have two methods, the first gets two time strings (one from a date picker, and the second from a time picker) and merges them into a single string like:
mergeTime(time, date) {
console.log(time);
console.log(date);
const dateString = `${date.getDay()}/${date.getMonth()}/${date.getFullYear()}`;
const timeString = `${time.getHours()}:${time.getMinutes()}:00`;
const merged = new Date(`${dateString} ${timeString}`);
console.log(merged);
return this.timestamp(merged);
}
Time string output:
Tue Apr 24 2018 15:00:00 GMT-0400 (Eastern Daylight Time)
Date string output:
Thu Apr 12 2018 02:42:07 GMT-0400 (Eastern Daylight Time)
However when I merge them, I am not seeming to get the correct output:
Tue Apr 03 2018 15:00:00 GMT-0400 (Eastern Daylight Time)
Finally I pass them through the last method to convert the merged time into milliseconds which seems to be working, but if you see a way i can do this differently!
timestamp(timeString) {
return new Date(timeString).getTime();
}
I want to convert following date and time, into a timestamp 13 digits. Note I use strict mode.
Date:
Thu Jun 01 2017 00:00:00 GMT+0200 (CEST)
Time:
03:00
I tried to use Date.parse() but it was not working. What is the best way to do this?
You can use the getTime function:
console.log(new Date('Thu Jun 01 2017 00:00:00 GMT+0200 (CEST)').getTime());
Try below code
console.log(new Date().getTime());
Create the date object, add time as you wish and then use the getTime() function:
"use strict";
var date = new Date('Thu Jun 01 2017 00:00:00 GMT+0200 (CEST)');
date.setHours(parseInt('03'),[parseInt('00')]); //apply hours and minutes
console.log(date.toString());
console.log(date.getTime()); //use of getTime()
console.log(+new Date("Thu Jun 01 2017 00:00:00 GMT+0200 (CEST)"))
if I console log the two I get the same result but for some reason the result of the third console is false which means there is some difference! Any help is appreciated.
console.log(new Date());
console.log(moment(new Date()).toDate());
console.log(moment(new Date()).toDate() == new Date());
The following is the result I get on the console:
Wed Jul 08 2015 15:55:30 GMT-0500 (Central Daylight Time)
Wed Jul 08 2015 15:55:30 GMT-0500 (Central Daylight Time)
false
You are comparing Date objects, but you should really be comparing their values:
console.log(moment(new Date()).toDate().getTime() == (new Date()).getTime());
console.log(+(moment(new Date()).toDate()) == +(new Date()));
More discussion on this here: Compare two dates with JavaScript
I'm trying to subtract 1 hour from the current date, I'm using this code:
var date = new Date();
// date is **Fri Apr 03 2015 16:47:33 GMT+0100 (GMT Standard Time)**
var uploadDateFilter = new Date(new Date(date).setHours(date.getHours()-1)).toISOString();
//now date is **Fri Apr 03 2015 14:47:33 GMT+0100 (GMT Standard Time)**
There's less two hours instead of just one. What am I missing here?
Your code works when looking at the elements in the same format.
<div class="original"></div>
<div class="updated"></div>
Using jQuery to post the information ... with your code, as is.
$(".original").text(date.toISOString());
$(".updated").text(uploadDateFilter);
Results in ...
2015-04-03T16:00:23.441Z
2015-04-03T15:00:23.441Z
http://jsfiddle.net/rfornal/5nma5uhr/
You don't need .toISOString(), but for me it works :
var date = new Date();
var uploadDateFilter = new Date(new Date(date).setHours(date.getHours()-1));
console.log(date);
console.log(uploadDateFilter);
Console output :
Fri, 03 Apr 2015 15:59:48 GMT
Fri, 03 Apr 2015 14:59:48 GMT
http://repl.it/gxL
The reason appears to be daylight saving check this my fiddle.
For me in the UK the clocks went forward on Mar 29, 2015. Checking the effect of new Date().toISOString() before and after this date, during daylight saving the result is one hour less.