Javascript UTC timezone does not work in Firefox - javascript

Below code doesn't work with Firefox, Works perfectly fine with Chrome. Can someone please help me to find an alternate solution?
const tempDate = getStartDate['startDate']; // Returns: 2020-08-13 12:52:38
new Date(`${tempDate} UTC`);
Expected Output: Thu Aug 13 2020 08:52:38 GMT-0400 (Eastern Daylight Time)

You're trying to do:
new Date('2020-08-13 12:52:38 UTC')
Instead you should do:
new Date('2020-08-13T12:52:38Z')
See https://www.ecma-international.org/ecma-262/11.0/index.html#sec-date-time-string-format

maybe you can use moment: https://www.npmjs.com/package/moment
var d = new Date(`Wed Aug 12 2020 18:34:35 GMT-0400 (Eastern Daylight Time) UTC`);
var date = moment(d).utc().format()
console.log("date: ", date )

Related

Weird date issue in javascript

I have a SharePoint list with delivery date column. I have created a table to display current week and next week delivery items using javascript. Everything works fine but for couple of team members Thursday delivery items are displaying in Wednesday cell and Friday items in Thursday cell.
I am not sure why it is happening. Can anyone help me out to resolve this issue? Any help would be greatly appreciated.
Here is my code. Added alerts to verify data, I am wondering why in second and third alerts sunday is showing Monday date and in third alert monday is showing Tuesday date. Added alert messages at the bottom. Please advice.
today = moment();
sundayDate = new Date(today.startOf('week'));
sundayShortDate = sundayDate.toLocaleDateString();
sundayTitle = getFormattedDate(sundayDate);
window.alert("sundayDate ::"+sundayDate+"");
monDate = new Date(sundayDate.setDate(sundayDate.getDate() + 1));
monSDate = monDate.toLocaleDateString();
monTitle = getFormattedDate(monDate);
window.alert("sundayDate ::"+sundayDate+"; monDate::"+monDate+"");
tueDate = new Date(monDate.setDate(monDate.getDate() + 1));
tuesSDate = tueDate.toLocaleDateString();
tueTitle = getFormattedDate(tueDate);
window.alert("sundayDate ::"+sundayDate+"; monDate::"+monDate+"; tueDate::"+tueDate+"");
First window alert:
sundayDate ::Sun Aug 16 2020 00:00:00 GMT-0400 (Eastern Daylight Time)
Second Alert:
sundayDate ::Mon Aug 17 2020 00:00:00 GMT-0400 (Eastern Daylight Time); monDate::Mon Aug 17 2020 00:00:00 GMT-0400 (Eastern Daylight Time)
Third Alert:
sundayDate ::Mon Aug 17 2020 00:00:00 GMT-0400 (Eastern Daylight Time); monDate::Tue Aug 18 2020 00:00:00 GMT-0400 (Eastern Daylight Time); tueDate::Tue Aug 18 2020 00:00:00 GMT-0400 (Eastern Daylight Time)
As was already commented, you are mutating the dates with calls to setDate().
As you already use moment, don't switch back to Date objects, but stick to moment objects.
For example:
let today = moment();
let fmt = "dddd, MMMM Do YYYY";
let sunDate = today.startOf('week');
let sunShortDate = sunDate.format(fmt);
let monDate = sunDate.clone().add(1, "day");
let monShortDate = monDate.format(fmt);
let tueDate = monDate.clone().add(1, "day");
let tueShortDate = tueDate.format(fmt);
console.log({ sunShortDate, monShortDate, tueShortDate });
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.0/moment.min.js"></script>
Just like Date objects, also moment objects are mutable, and so you should call clone first before calling add.
You set sundayDate to sundayDate.setDate(sundayDate.getDate() + 1)
You should do:
monDate = new Date(sundayDate.getDate() + 1);
P.S.
I'm not sure if mixing moment.js and js Date is a good idea.

If statement with date comparison

I'm trying to write an if statement that runs some code if the date is after April 24th, 2017, 10 am EDT, but it doesn't appear that my variables are comparable (different data types?).
I'm trying to avoid using Moment.js for just this.
var today = new Date();
var launch = 'Mon Apr 24 2017 10:00:00 GMT-0400 (EDT)';
today returns Tue Apr 04 2017 14:34:41 GMT-0400 (EDT).
When I test if either is greater than the other, both are false. How should I format my dates?
Thanks!
You have to have launch as a date type:
var today = new Date();
var launch = 'Mon Apr 24 2017 10:00:00 GMT-0400 (EDT)';
var launchDate = Date.parse(launch);
if ( launchDate > today )
You should also read more about dates here:
Compare two dates with JavaScript

date string that will ignore timezone offset

I am at (UTS-05:00) Eastern Time (US & Canada)
i.e, new Date().getTimezoneOffset() == 300 seconds.
Now, I have a API endpoint (JSON) that returns a date string like this.
{
someDate: '2016-01-01T00:40:00.000+00:00'
}
Here, I pass it to Date constructor like this
var dateString = "2016-01-01T00:40:00.000+00:00";
var someDay = new Date(dateString);
console.log(someDay)
Mozilla Firefox console shows
Date {Fri Jan 01 2016 00:40:00 GMT-0500 (Eastern Summer Time)}
Google Chrome console shows
Thu Dec 31 2015 19:40:00 GMT-0500 (Eastern Standard Time)
Chrome is taking the TimezoneOffset into consideration and Firefox is not. What can I do to get a Date that doesn't take Offset into consideration like FireFox in Chrome?
You can do it by:
var dates = '2016-01-01T00:40:00.000+00:00'.split(/-|T|:/);
var newDate = new Date(dates[0], dates[1]-1, dates[2], dates[3], dates[4]);
This hack works (not very clean, but does the job)
var dateString = '2016-07-27T01:40:30';
var dateParts = dateString.split(/-|T|:/);
var saneDate = new Date(
+dateParts[0],
dateParts[1] - 1,
+dateParts[2],
+dateParts[3],
+dateParts[4],
+dateParts[5]);
console.log(saneDate);

convert date from 23-08-2015 00:00:00 to Tue Aug 23 2015 00:00:00 GMT+0530

I have date in 23-08-2015 00:00:00 format in my controller and pass it to view using ViewData. I want to convert this date to Tue Aug 23 2015 00:00:00 GMT+0530 format.. Is it possible with controller or can I convert it in my view using jquery?
Can anyone help me to solve this?
In javascript, if you simply create a new Date object with the string that you have, you'll get the desired format.
var testdate = new Date("23-08-2015 00:00:00");
console.log(testdate);
Output:
Tue Nov 08 2016 00:00:00 GMT+0530 (India Standard Time)
In your controller:
DateTime date = DateTime.Now;
date.ToLongDateString();
I done it by using following format:
string startDateCalendar = Convert.ToString(startDate.ToString("ddd MMM dd yyyy HH:mm:ss")) + " GMT+0530";
using JavaScript it is like so:
function parseDDMMYYYY(d){
return new Date(
d.replace(/^(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/,"$3-$2-$1 $4:$5:$6")
)
}
parseDDMMYYYY('23-08-2015 00:00:00')

Compare dates issue - javascript

I need to compare dates in javascript.
After attempt many ways...
I choose:
var endDate = new Date(secondDate.getYear(), secondDate.getMonth(), secondDate.getDate(), 0, 0, 0,0);
var startDate = new Date(firstDate.getYear(), firstDate.getMonth(), firstDate.getDate(), 0, 0, 0, 0);
if (endDate.getTime() >= startDate.getTime()) {
isValid = true;
}
else {
isValid = false;
}
In my situation:
---startDate = Tue Apr 01 1997 00:00:00 GMT+0200 (Jerusalem Standard Time) (i.e, 01/04/1997)
---endDate = Thu Jul 26 114 00:00:00 GMT+0200 (Jerusalem Standard Time) (i.e, 26/07/2014)
You see? startDate is small then endDate, right?
But:
---endDate.getTime() returns: -58551904800000
---startTime.getTime() returns: 859845600000
so, endDate.getTime() >= startDate.getTime() returns false...
In other situation, it works well:
---startDate: Sat Jul 21 114 00:00:00 GMT+0200 (Jerusalem Standard Time) (i.e, 21/07/2014)
---endDate: Sat Jul 28 114 00:00:00 GMT+0200 (Jerusalem Standard Time) (i.e, 28/07/2014)
---startDate.getTime() returns -58552336800000
---endDate.getTime() returns -58551732000000
so, endDate.getTime() >= startDate.getTime() returns true...
It seems like that javascript functions have another behavior for dates after year 2000.
What should I do? which code will be match to all of the optional situations?
Thanks.
Yeah like ghusse said, there is a problem with your end time if you fixed it so it was 2014 you would get a result such as 1406329200000 instead of -58551904800000
I found a solution, after I read Josh and ghusse answers and advice:
Use getFullYear(), instead getYear(), and all will work O.K.
Apparently, you have a problem with your end dates :
Thu Jul 26 114 00:00:00 GMT+0200
Does not mean 21/07/2014 but 21/07/114
According to the doc, here are 2 correct ways of creating your date:
var endDate = new Date(21, 6, 2014);
// Or a string corresponding to a version of ISO8601
var endDate = new Date('2014-07-21T00:00:00z+3');

Categories

Resources