method to merge date strings not working correctly - javascript

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();
}

Related

How to convert Date format like this Fri Apr 20 2020 00:00:00 GMT+0530 (India Standard Time) to 2020-04-20T00:00:00.000Z in Javascript?

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());

How to get `Fri Oct 25 2019` from a date object (Fri Oct 25 2019 15:27:01 GMT+0530 (India Standard Time))?

How can I get Fri Oct 25 2019 from a date object as below?
Fri Oct 25 2019 15:27:01 GMT+0530 (India Standard Time)
Use the toDateString() function from JavaScript.
let d = new Date();
// note : the actual display output depend on your browser/system settings (locale)
console.log(d.toString());
console.log(d.toDateString());
This will take the first "date only" part of the string that is displayed from the date object.
It could also be done with string manipulation, but this function is actually intended for this usecase, so probably clearer.
Doc for toDateString function

How to build Time object using a date string as Local Time

All:
Thanks for help. I wonder how can I build a Date object using local time string, for example:
If I use new Date("2016-07-01"), what I want to build is
2016-07-01 00:00:00 GMT-0800 (Pacific Standard Time) (say I am in San Francisco),
but right now, it gives me something like
Thu Jun 30 2016 16:00:00 GMT-0800 (Pacific Standard Time)
Any idea?
To convert the default UTC time created from running new Date(dateString):
const MILLISECONDS_PER_MINUTE = 60000;
const utcDate = new Date('2015-01-01');
console.log(new Date(utcDate.getTime() + (utcDate.getTimezoneOffset() * MILLISECONDS_PER_MINUTE))); // Wed Jan 01 2014 00:00:00 GMT-0700 (MST)

Why is new Date() subtracting 1 day in Javascript?

I need to convert a String to a Date object.
The date string is delivered in the following format:
"2015-01-28T00:00:00"
When I create a new Date, I get the previous date:
Entered: new Date("2015-01-28T00:00:00")
Result: Tue Jan 27 2015 17:00:00 GMT-0700 (Mountain Standard Time)
Does anyone know why this is occurring?
When you enter the following:
new Date("2015-01-28T00:00:00");
// Result: Tue Jan 27 2015 17:00:00 GMT-0700 (Mountain Standard Time)
the browser assumes that you are giving a date in GMT Time zone. So it will automatically convert the given date to your local date.
It's always a good idea to inform the browser of the timezone you are working on in order to prevent future problems:
new Date("2015-01-28T00:00:00-07:00");
// Result: Tue Jan 28 2015 00:00:00 GMT-0700 (Mountain Standard Time)
Actually, you aren't getting the previous date . . . you are getting that date, offset by the timezone difference.
Tue Jan 27 2015 17:00:00(Mountain Time) + 7 hours (time zone difference) = 2015-01-28T00:00:00 (GMT)
Or, in English, when it is 12:00 Midnight in Greenwich, England, it is 5:00 PM on the previous day in Denver, Colorado. ;)
It's the right date/time, just in a different timezone.

Chrome browser new date("2014-03-18T10:52:42.55") thanged the time?

new Date("2014-03-18T10:52:42.55");
result:
Tue Mar 18 2014 06:52:42 GMT-0400 (Eastern Daylight Time)
My time : 10:52:42.55
Result time: 06:52:42 GMT-0400 (Eastern Daylight Time)
How to get the result in GMT000?
I have to show date and time separately.
It is unclear what you want- to set the date at GMT timezone, suffix the string with a 'Z':
new Date("2014-03-18T10:52:42.55Z");
/* Tue Mar 18 2014 06: 52: 42 GMT-0400(Eastern Daylight Time)
*/
If you want to return a string to display UTC time use
new Date("2014-03-18T10:52:42.55Z").toUTCString()
and parse the return,
or use the Date object methods like D.toUTCDate(), D.toUTCHours()

Categories