Javascript Formatting date not working - javascript

I am using the ionic framework and I am trying to calculate the number of days between today and Sun Apr 18 2017 15:14:36 GMT+0800 (MYT)
and today :Thu Mar 09 2017 17:52:48 GMT+0800 (MYT)
it's showing that there are 44 days but I know it's 40 days. Can someone help me fix my code
$scope.daysLeft=Math.floor(Math.abs(newDate - new Date())/86400000);

You are wrong and the result is actually ok from your program.
There are 44 days between today and the selected date.
Just try here if you don't believe it.

Related

How can I round seconds to 0 with dayJS

I am creating an app which asks the users for 2 dates, then it calculates the time between them dates.
But the dates have seconds in them so instead of 90 minutes I am getting 89 minutes.
So I can convert this:
startTime: Sat Apr 09 2022 09:00:39 GMT+0100 (Irish Standard Time)
Into this:
startTime: Sat Apr 09 2022 09:00:00 GMT+0100 (Irish Standard Time)
I would preferably do this with DayJS
I came up with a solution with the help of James!
values.startTime = dayjs(time[0].setUTCSeconds(0)).toDate();
Thank you!

Moment JS and BST

Going out of mind with this. So I have the following:
var now = moment().local();
var timestamp = now.unix();
But the timestamp is always one hour behind and I cant figure out why. If I log now in the console I get what I expect:
Tue Jun 09 2020 19:19:32 GMT+0100 (British Summer Time)
But when I log timestamp to the console I get
1591726782
Which equates to:
Tue, 09 Jun 2020 18:19:32 GMT
Driving me completely potty. Probably a really simple solution but I have trawled the web and checked MomentJS documentation and cant see where I am going wrong. Any help would be greatly appreciated.

what does the T separator in JavaScript Date

I think I need clarification on something:
I have a string representing a date in a format like this:
'2013-12-24 12:30:00'
and if I pass it to Date(), then I get the following output
new Date('2013-12-24 12:30:00')
// --> Tue Dec 24 2013 12:30:00 GMT+0100
because iOS has problems with this, I read that I should use T as separator, however
new Date('2013-12-24T12:30:00')
// --> Tue Dec 24 2013 13:30:00 GMT+0100
the result adds one hour. I guess it has something to do with summer or winter, but what exactly does the T stand for, and why is the result different? I meanwhile solved my problem by passing separate parameters to the Date but I would still like to know where this extra hour is coming from.
new Date('2013-12-24T12:30:00')
treats the time as UTC, so it's 12:30 in Greenwich and 13:30 in your timezone.
new Date('2013-12-24 12:30:00')
is a Chrome extension (or bug) that doesn't work in other browsers. It treats the time as local, so it's 12:30 in your timezone (GMT+1) and 11:30 in Greenwich.
If you look closely... you will notice adding a T makes the time in 24-hour format.
new Date('2013-12-24T12:30:00')
// --> Tue Dec 24 2013 13:30:00 GMT+0100
Compared to
new Date('2013-12-24 12:30:00')
// --> Tue Dec 24 2013 12:30:00 GMT+0100
I guess its stands for Long time pattern. Refer this for more.

Javascript huge date bug in windows... Solutions?

My dev environment is in mac.
I use bootstrap datepicker to choose dates in an application.
When a day like 27/11/2013 is selected in mac when I debug, the screen shows...
day: 1385510400
and the translation to an date object in the same debug tool is,
Wed Nov 27 2013 00:00:00 GMT+0000 (GMT)
which is correct.
All is OK, since here.
I upload the code to a windows environment and open the same page with IE8.
Start the nightmare... :)
In windows, the same day variable is shown like that...
day: 1385506800
and the translation to an date object in the same debug tool is,
Wed Nov 27 2013 00:00:00 GMT+0000 (GMT)
which, is not correct?
If we go to a external tool, the day that gave IE8 is day before, and the translation should be,
Tue, 26 Nov 2013 23:00:00 GMT.
How I can understand this?
There is no relation with the timestamp and the translation to date object, and obviously I can't find a way to make this works in both systems.
Could you wake me up from this nightmare?
Sorry for my english!
Fiddle
In JS in Chrome:
new Date(1385510400000) : Wed Nov 27 2013 01:00:00 GMT+0100
new Date(1385506800000) : Wed Nov 27 2013 00:00:00 GMT+0100
new Date("11/27/2013") : Wed Nov 27 2013 00:00:00 GMT+0100
Please note the order of the date string, it is in US order
Also note the 000 at the end; JS timestamps are in milliseconds
The same in IE8
Wed Nov 27 01:00:00 UTC+0100 2013
Wed Nov 27 00:00:00 UTC+0100 2013
Wed Nov 27 00:00:00 UTC+0100 2013
So exactly the same dates, on hour difference on the first
If you subtract the 1 hour from all dates, you get midnight on the first and 11pm on the 26th on the rest
To normalise you can do
var d = new Date(1385510400000);
d.setHours(0,0,0);
or change to UTC

DateJS parsing mystery

I'm using DateJS to parse user-inputted dates, and getting some strange results.
Date.parse("15 Jan 2010") returns Fri Jan 15 00:00:00 EST 2010 (right)
Date.parse("15-Apr-2010") returns Thu Apr 15 00:00:00 EDT 2010 (right)
Date.parse("15 Apr 2010") returns Thu Apr 1 00:00:00 EDT 2010 (wrong)
As far as I can tell, the d MMM yyyy input format works fine for every month except April and August; in those two cases, it returns the first of the month no matter what day is entered. Is this a bug, or is there a logical explanation I'm missing?
Aha: Looks like the version in the "Download" link is a good bit older than the current source. Here's the commit that fixed this bug:
Dan Yoder fixed bug with timeContext pattern where if a date included
"april" or "august", the parser thought the 'a' was the beginning of a time part
(as in am/pm).
The most recent version of the EN-US script is here:
http://code.google.com/p/datejs/source/browse/trunk/build/date-en-US.js
It would be nice if the website linked to this instead of to a zip file that hasn't been updated for a couple of years.

Categories