getting incorrect time while converting string into date object - javascript

I am using calling this function getFormattedTimeFromString(startTime)
getFormattedTimeFromString(timeString){
return (new Date('1970-01-01T' + timeString + 'Z'));
}

if you are passing in getFormattedTimeFromString("14:00:00") and getting Thu Jan 01 1970 19:30:00 GMT+0530 (IST) as Output well then that is expected...
What do you want it to be? Maybe you want to remove the Z? for it to be local?
the Thu Jan 01 1970 19:30:00 GMT+0530 (IST) is just a representation in your local timezone
while i get Thu Jan 01 1970 15:00:00 GMT+0100 (CET)
but it still being the same in UTC.
If you would do:
new Date(`Thu Jan 01 1970 19:30:00 GMT+0530 (IST)`).toJSON()
// you get same input back
"1970-01-01T14:00:00.000Z"
which is still the same input
You also have diffrent method on the date object like getUTCxxxx if that is what u want

Related

DateTime value from backend to frontend

I've been struggling for days with some DateTime values.
I have an API backend that uses entity framework and sql server with .netcore.
The big issue when i want to send a datetime from angular to c#
backend. I noticed that Date() in typescript/javascript by default
uses my timezone and i don't know how to exclude it.
For example my date looks like this:
Wed Jul 11 2019 21:00:00 GMT+0300
And when it arrived in c# it becomes 07/10/2010(mm-dd-yyyy), it subtracts 1 day due to timezone.
Is there a way to standardize the Date variable to ignore timezone and always keep the same format DD-MM-YYYY ?
I've also tried to use MomentJS and still can't figure it out, even my MomentJS compares are acting strange due tot his issue.
For example:
const VacationStart = moment(calendarEntity.Vacation.StartTime).utc(false);
const VacationEnd = moment(calendarEntity.Vacation.EndTime).utc(false);
if (VacationStart.isSameOrBefore(ColumnDate,'day') && VacationEnd.isSameOrAfter(ColumnDate,'day')) {
return '#FF0000';
}
In the above example:
VacationStart is Wed Jul 10 2019 21:00:00 GMT+0300
VacationEnd is Wed Jul 17 2019 00:00:00 GMT+0300
ColumnDate is Thu Aug 15 2019 03:00:00 GMT+0300 (incremental value)
Yet for some reason even if i use isSameOrBefore(ColumnDate,'day') to specify to compare only up to days it still does not work. When VacationEnd should be equal to ColumnDate is return false.
Note: everything is in a foreach loop where ColumnDate increases by +1 day.
You just need to use UTC time (Greenwich Mean Time)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC
https://learn.microsoft.com/en-us/dotnet/api/system.datetime.utcnow?view=netcore-2.2
So something like this:
new Date(new Date().toUTCString()); -- "Mon Jul 01 2019 17:55:41 GMT-0700 (Pacific Daylight Time)"
new Date().toUTCString(); -- "Tue, 02 Jul 2019 00:56:38 GMT"
new Date().toString(); -- "Mon Jul 01 2019 17:57:03 GMT-0700 (Pacific Daylight Time)"

Create javascript Date() object to Amrica/New_York timezone

The code:
console.log(start)
reads, Thu Mar 01 2018 00:00:00 GMT+0530 (India Standard Time)
I want it a new object start_NY which will read Thu Mar 01 2018 00:00:00 w.r.t. America/New_York timezone. Something like Thu Mar 01 2018 00:00:00 GMT-0500.
I used a script:
start_ny = new Date('Thu Mar 01 2018 00:00:00 GMT-0500');
But that reads, Thu Mar 01 2018 10:30:00 GMT+0530 (India Standard Time), which is actually converting the date into Indian Standard Time, instead of giving me a time from New_York timezone.
The format should be same as that of the existing one. How can I do that?
Try with this following code and MDN resources
new Date().toLocaleString('en-US', { timeZone: 'America/New_York' })
// Date Format
new Date().toDateString('en-US', { timeZone: 'America/New_York' });

.split returns NaN by converting string into array

I need to convert a string, which contains several dates, into an array.
This works perfectly fine as long as the string contains of numbers.
The string containing information on the dates looks as following:
Wed Nov 01 2017 13:06:56 GMT+0100 (CET),Wed Nov 01 2017 13:07:10 GMT+0100 (CET),Wed Nov 01 2017 13:07:12 GMT+0100 (CET),Wed Nov 01 2017 13:07:13 GMT+0100 (CET),Wed Nov 01 2017 13:07:15 GMT+0100 (CET),Wed Nov 01 2017 13:07:16 GMT+0100 (CET)
My code looks as following:
var times = string.split(",").map(Number);
console.log(times[0]);
Unfortunately, this results in:
NaN
I cannot explain why it is not working. Even if I replace
","
by
"GMT+"
it is still not working. Thanks for your help in advance!
Splitting the string is working as it should.
The problem is that by calling .map(Number), you are trying to pass each element of the array to the Number constructor. This returns NaN, since the strings can't be converted directly into numbers.
For example:
new Number('Wed Nov 01 2017 13:06:56 GMT+0100 (CET)'); // NaN
What you probably want is .map(Date.parse), since the Date.parse function can handle strings on this format. The result will be the "number representing the milliseconds elapsed since January 1, 1970, 00:00:00 UTC" (MDN)
You don't actually need to use map() at all; simply using
var times = string.split(",");
will give you the times split in the times array, which you can access with times[0]:
var string = 'Wed Nov 01 2017 13:06:56 GMT+0100 (CET),Wed Nov 01 2017 13:07:10 GMT+0100 (CET),Wed Nov 01 2017 13:07:12 GMT+0100 (CET),Wed Nov 01 2017 13:07:13 GMT+0100 (CET),Wed Nov 01 2017 13:07:15 GMT+0100 (CET),Wed Nov 01 2017 13:07:16 GMT+0100 (CET)';
var times = string.split(",");
console.log(times[0]);
This is what you need I guess:
ES6
let times = string.split(',').map(d => new Date(d));
ES5
var times = string.split(',').map(function (d) { return new Date(d)});

Date returning inconsistent results (depending whether leading zero exists)

> new Date('2015-1-1')
Thu Jan 01 2015 00:00:00 GMT-0500 (EST)
> new Date('2015-01-1')
Thu Jan 01 2015 00:00:00 GMT-0500 (EST)
> new Date('2015-1-01')
Thu Jan 01 2015 00:00:00 GMT-0500 (EST)
// Yet...
> new Date('2015-01-01')
Wed Dec 31 2014 19:00:00 GMT-0500 (EST)
// Similarly:
> new Date('2015-1-10')
Sat Jan 10 2015 00:00:00 GMT-0500 (EST)
> new Date('2015-01-10')
Fri Jan 09 2015 19:00:00 GMT-0500 (EST)
Can't figure out why this is happening (Chrome 39). Is it related to octal parsing?
Firefox only accepts new Date('2015-01-10'), and returns what I expect: Date 2015-01-10T00:00:00.000Z
Found the answer in a related question; it appears Chrome parses the YYYY-MM-DD format as UTC time, then converts it the local timezone. So, 2015-01-01 00:00:00 in UTC is Dec 31 in EST.
See Inconsistencies when creating new date objects:
It looks like the form '1979-04-05' is interpreted as a UTC date (and then that UTC date is converted to local time when displayed).
Apparently, a possible cross browser solution is to replace the dashes with slashes to force using local time:
new Date('2015-01-10'.replace(/-/g, '/'))
I am unsure of your problem since My chrome(39.0.2171.99) gives me Jan 01 in all case. But having said this, I would like to point out that you should probably use
new Date(2015,1,1)
This is how JS Date is supposed to be initialised.

JavaScript get date and day using Date.parse

by using:
Date.parse('2015-01-01');
it gives output: Thu Jan 01 2015 05:00:00 GMT+0500 (Pakistan Standard Time)
but i want just like this: Thu Jan 01 2015
(moreover i am using it in morris.js chart)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Conversion_getter
new Date('2015-01-01').toDateString();

Categories

Resources