Wrong day paramenter in new Date()? - javascript

Could someone explain me, why I get 12 Februray when running the code below?
I saw the days are from 1 to 31, only the months starts with 0
var d = new Date(2100,1,13)
> d
Fri, 12 Feb 2100 23:00:00 GMT
EDIT:
And why this time?? 23:00:00 it should be 00:00:00

Your locale timezone is interfering. Try: new Date(Date.UTC(2100,1,13)).

You are setting 13 Feb 2100 CET and getting output in GMT.

The output is based on the GMT zone and not as per your time zone. Adjust your system time to proper time zone, you should be getting correct output. Hope that helps.

Related

Moment js time stamp

I am using moment.js to parse my date time and I am getting totally confused.
If I do moment().toDate() I get:
Fri Aug 21 2020 21:52:48 GMT-0400 (Eastern Daylight Time)
which is exactly correct. I am showing this so that you know that moment is not pulling incorrect local time for me.
I have a time which looks like this that I pulling from a database:
2020-08-21T21:49:58.000Z
As you can see this is the same day as above but a slightly different time like 3 minutes ago.
If I do:
moment("2020-08-21T21:49:58.000Z").calendar()
it returns :
"Today at 5:49 PM"
which is incorrect.
However, if remove 0Z from the end of the time stamp and do this:
moment("2020-08-21T21:49:58.00").calendar()
I get
"Today at 9:49 PM"
which is correct.
I am guessing by looking at the format moment decides it is a UTC time and subtract 4 hours from it.
Therefore, I thought I would convert it back to utc() but when I do:
moment("2020-08-21T21:49:58.000Z").utc().calendar()
I get:
"Yesterday at 9:49 PM"
which decreases it by 24 hours and I am not sure why.
Is there a function that I can use to prevent it from assuming that the time is in UTC format?
Edit 1:
If I do moment().toDate() I get:
Sun Aug 23 2020 20:53:28 GMT-0400 (Eastern Daylight Time)
which is correct.
When I try to use the time stamp from today:
moment("2020-08-23T20:52:38.000Z").utc().calendar()
I am getting:
"Yesterday at 8:52 PM"
You could try to enable your moment object to local time mode:
moment("2020-08-21T21:49:58.000Z").local().calendar()

Angular min date validation incorrect for any year before 1901

Any help or explanation for this would be really appreciated. I've got to believe this is an issue with my implementation because otherwise it seems like a huge bug in Angular.
Setup
Define a form with a min date of 0001-01-01 (to get around this Android date performance issue)
<input type="date" min="0001-01-01" max="9999-12-31"/>
Problem
Now the date control does not believe any dates prior to 1901 to be valid e.g. 1800
Additional Info
Replicated using Angular's own date Plunker.
I also found that a min year of 0100 made 1800 valid, while a min year of 0010 made 1800 not.
Any way around this?
Welcome to the wonderful world of js dates, you will end up pulling your hair out.
console.log(new Date('01/01/10'));
Fri Jan 01 2010 00:00:00 GMT+0100 (West-Europa (standaardtijd))
console.log(new Date('01/01/100'));
Fri Jan 01 100 00:00:00 GMT+0100 (West-Europa (standaardtijd))
console.log(new Date('01/01/0001'));
Mon Jan 01 2001 00:00:00 GMT+0100 (West-Europa (standaardtijd))
I think new Date('01/01/100') is the lowest possible JS date. (timezone excluded)

Javascript Date issue, incorrect timezone

I got this strange JavaScript bug that I can seem to work arround or fix.
I am using some code to make 2 JavaScript dates, to insert events into a calendar component.
The dates are built the following way:
var endDate = new Date();
var startDate = new Date();
startDate.setDate(startDateDay);
startDate.setMonth(startDateMonth);
startDate.setFullYear(startDateYear);
startDate.setHours(2, 0, 0, 0);
endDate.setDate(endDateDay);
endDate.setMonth(endDateMonth);
endDate.setFullYear(endDateYear);
endDate.setHours(2, 0, 0, 0);
So, the dates are built using integers. These integers are determined by input, and using the debugger I can see 100% positive they are coming in correctly.
Now, ill describe 3 walkthroughs, 2 where it goes correctly and 1 where it goes wrong.
Using the following input:
endDateDay = 20
endDateMonth = 9
endDateYear = 2014
Gives the following date object as result:
Tue Oct 20 2014 02:00:00 GMT+0200 (W. Europe Daylight Time)
Using this input:
endDateDay = 13
endDateMonth = 9
endDateYear = 2014
Gives the following date object as result:
Tue Oct 13 2014 02:00:00 GMT+0200 (W. Europe Daylight Time)
Now, using this input:
endDateDay = 27
endDateMonth = 9
endDateYear = 2014
Gives the following date object as result:
Mon Oct 27 2014 02:00:00 **GMT+0100** (W. Europe Standard Time)
As you can see, for some strange reason the TimeZone is off. This gives errors in my application, and I need to find a way to get it fixed. Though, I cannot find any solution to it, let alone understand why it is actually happening.
PS: I am using Google Chrome
The answer was indeed the difference in the daylight savings time, which I completly oversaw. Thanks to finding this out I also found a solution to my problem.
I used this link to further assist me, might it help someone in the future:
http://javascript.about.com/library/bldst.htm
Cheers!

Convert GMT time to local time

Am getting a GMT time from my server in this format
Fri, 18 Oct 2013 11:38:23 GMT
My requirement is to convert this time to local time using Javascript, eg:/ if the user is from India, first i need to take the time zone +5.30 and add that to my servertime and convert the time string to the following format
2013-10-18 16:37:06
I tried with following code but not working
var date = new Date('Fri, 18 Oct 2013 11:38:23 GMT');
date.toString();
Please help me to solve this issue, Thanks in advance
This worked for me:
<script>
var strDateTime = "Fri, 18 Oct 2013 11:38:23 GMT";
var myDate = new Date(strDateTime);
alert(myDate.toLocaleString());
</script>
Please take a look at http://www.w3schools.com/jsref/jsref_obj_date.asp for all further date time manipulations, from the date object myDate.

Javascript date formatting - one hour out due to daylight saving

So now, its 9:23am. I have a UTC date string that represents the current date, that looks like this "2012-07-17T09:23:27.75"
I want that in a date object, so I can display a nicely formatted date, so I:
var myDate = new Date("2012-07-17T09:23:27.75")
// Gives --> Tue Jul 17 2012 10:23:27 GMT+0100 (GMT Daylight Time)
So because of daylight saving time I'm getting an hour-out issue. I can see that myDate.getTimezoneOffset() gives me -60, what's the standard / best practice way to get my date to actually reflect the current correct time? Have I just entered javascript date hell?
Try momentjs.com. I really found it handy for such things.
var myDate = moment("2012-07-17T09:23:27.75");
Gives you a date instance in your timezone (that basically configured on your computer). Moreover momentjs has nice human friendly formattings like "a couple of seconds ago", "a month ago",...
Dates are really a hell in JS (but not only in JS). The best thing you can do is to always only transport in UTC between browser <-> server. Then on the server convert it to what time format you like, you obviously only have to be consistent. That way I managed to handle date-times properly.
Try removing the 'T'
I was debugging some date time format issue in chrome when I found out that in console
new Date('2016-04-16T15:15:00') returns Sat Apr 16 2016 16:15:00 GMT+0100 (GMT Daylight Time)
while
new Date('2016-04-16 15:15:00') returns Sat Apr 16 2016 15:15:00 GMT+0100 (GMT Daylight Time)

Categories

Resources