Date-time stamp conversion in javascript - javascript

I have Date String Thu May 23 2013 18:19:32 GMT+0530 (India Standard Time) from my database. I want to make in this format THURSDAY May 23 2013 18:19:32 GMT 0500 CDT in ext-js.any idea ? Thanks in advance.

There are 2 excellent ate parsing libraries available tat you can use. They are both very small
https://code.google.com/p/datejs/
http://momentjs.com/
Sample datejs usage:
Date.parse('Thu, 1 July 2004 22:30:00 GMT') // Thu Jul 01 2004 16:30:00
You can then format the date object in whatever format you require for output.

Related

GSheets GS Dates are one day off for 3/9/2020 - 4/5/2020 all other days are correct

GS code comes back one day off when getting date from 3/9/2020 - 4/5/2020
All dates between 3/9/2020 - 4/5/2020 comeback incorrect.
Google sheet add date column with date 3/9/2020
Add code to gs below
Comes back: Sun Mar 08 2020 23:00:00 GMT-0600 (CST)
3/9/2020
Sun Mar 08 2020 23:00:00 GMT-0600 (CST)
4/5/2020
Sat Apr 04 2020 23:00:00 GMT-0600 (CST)
var data = SpreadsheetApp.getActiveSpreadsheet().getDataRange().getValues();
SpreadsheetApp.getUi().alert(data[0][0]);
Here is the google sheet: link
The dates come back correctly if I format it using GMT.
var formattedDate = Utilities.formatDate(data[0][0], "GMT", "yyyy-MM-dd'T'HH:mm:ss'Z'");
UTC, GMT and Daylight Saving Time
Neither UTC nor GMT ever change for Daylight Saving Time (DST). However, some of the countries that use GMT switch to different time zones during their DST period.
For example, the United Kingdom is not on GMT all year, it uses British Summer Time (BST), which is one hour ahead of GMT, during the summer months.

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)"

Strange date parsing issue

I'm loading some dates comming from my database into a HTML table in a string format. The string looks like 31-AUG-13 I'm parsing this string into a date object using the below code:
var paymentDate = $(this).find('td.paymentDate').text();
var test = $.datepicker.parseDate('d-M-y', paymentDate);
Everything is ok so far and I'm getting this date object: Date {Sat Aug 31 2013 00:00:00 GMT+0300 (FLE Standard Time)} But once the year is bigger than 2023. In my case 31-JAN-24 and so on it is turning to 1924 and not 2024, so I'm getting these date objects:
Date {Thu Jan 31 1924 00:00:00 GMT+0200 (FLE Daylight Time)}
Date {Fri Feb 29 1924 00:00:00 GMT+0200 (FLE Daylight Time)}
Date {Mon Mar 31 1924 00:00:00 GMT+0300 (FLE Standard Time)}
Date {Wed Apr 30 1924 00:00:00 GMT+0300 (FLE Standard Time)}
And so on. My question is regarding this strange issue. Is there a way to declare the year range and why it is going back to 1900 in the case when the year is bigger than 2023?
This teaches a lesson always use year in full format same was case with y2k problem. Convert date from database in to yyyy format then use it.

Javascript Date constructor uses arbitrary time

When I construct a date object from a string, I am getting confusing results. It seems as if the time is chosen arbitrarily (but repeatably) if I don't specify it.
var d1=new Date("2013-10-9"), d2=new Date("2013-10-10");
output = d1+' '+d1.toUTCString()+'<br>\n';
output += d2+' '+d2.toUTCString()+'<br>\n';
Chromium 20.0...
Wed Oct 09 2013 00:00:00 GMT-0600 (MDT) Wed, 09 Oct 2013 06:00:00 GMT
Wed Oct 09 2013 18:00:00 GMT-0600 (MDT) Thu, 10 Oct 2013 00:00:00 GMT
Why would Chromium choose a different time on October 10?
By the way, the workaround is here: https://stackoverflow.com/a/744134/86967
It has to do with the format of the date string you are using. If you specify 2013-10-09 (notice the extra 0 on the day), then it works as expected. If you use 2 digits for the day and month, then you are following the ECMA spec.
var d1=new Date("2013-10-09"), d2=new Date("2013-10-10");
console.log(d1+' '+d1.toUTCString());
console.log(d2+' '+d2.toUTCString());
Yields:
Tue Oct 08 2013 20:00:00 GMT-0400 (Eastern Daylight Time) Wed, 09 Oct 2013 00:00:00 GMT
Wed Oct 09 2013 20:00:00 GMT-0400 (Eastern Daylight Time) Thu, 10 Oct 2013 00:00:00 GMT
I believe the code they are using can be found here:
https://github.com/WebKit/webkit/blob/master/Source/WTF/wtf/DateMath.cpp
When you provide an ECMA date, it will use the parseES5DateFromNullTerminatedCharacters method to parse the date, but when you use a non-standard date format it will use the parseDateFromNullTerminatedCharacters method. I am not that familiar with the webkit code, so I could be wrong, but this is based on my reading of the parsing logic.
The standard date format can be found in section 15.9.1.15 of the ECMA Spec.

javascript: new date, how to set default year?

I have a set of dates w/ or wo/ year, like the following:
10/1/2010
10/1
1-Oct
1-Oct 2006
Jan 4
Jan 4, 2008
When I tried to parse them using new Date(),
for those date that don't have year, I got year as 2001
e.g.
a=new Date("Jan 4")
Thu Jan 04 2001 00:00:00 GMT-0500 (EST)
I want to parse all the dates. If they have year, use that year(2010, 2006, 2008 in the above dates). otherwise, use 2011
Anyone can help?
Thanks,
Cheng
Use DateJS for parsing dates. The Date constructor's parser is very limited.

Categories

Resources