I am using date.js in my project, and I am encountering something I didn't expect with respect to dates that had time components of exactly noon or exactly midnight.
When I use the native javascript Date.parse method, I get the result I expected when parsing a date:
// plain old javascript
var date = new Date(Date.parse("10/21/2010 12:00:00 PM"));
alert(date);
I get this:
Thu Oct 21 2010 12:00:00 GMT-0500 (Central Daylight Time)
I interpret this as noon on October 21, 2010, which is what I think my input date is. However, when I import date.js and try something similar,
// using date.js
var date = Date.parseExact("10/21/2010 12:00:00 PM", "M/d/yyyy hh:mm:ss tt");
alert(date);
I get this:
Fri Oct 22 2010 00:00:00 GMT-0500 (Central Daylight Time)
This looks to me like midnight on the following day. I have no idea why it's interpreting this as midnight, or at any rate why it's interpreting this differently than the default Date.parse in javascript. Am I confused over the meaning of 12:00 PM? Am I misusing date.js's parseExact?
Looks like a version issue:
http://jsfiddle.net/4QK8Q/
http://jsfiddle.net/JKg3n/
The first one is your file (from the google code base), the second is from their website (datejs.com). Funnily enough they look like the same version, but obviously aren't. I'm not familiar enough with the library to tell what's going on, but I suggest filing a bug report.
This is a fairly old question, but for those who may stumble upon this in a search, it's highly suggested that you switch to the currently maintained version of Date.js (https://github.com/abritinthebay/datejs/) if you're encountering unexpected behavior.
The versions on the Google code base and datejs.com both have unfixed bugs that are unlikely to ever be addressed, as the original developer seems to have abandoned the project.
Related
I am having some serious issues with the Date object from the browser. It always seems to return the Summer time instead of keeping account of DST. I tried many solutions but with no avail. I am using moment, moment-timezone but none seem to help me return just the right offset so I can adjust the time sent to the server accordingly. Any ideas how to solve this?
Example: getTimezoneOffset() returns a value of 120, however the time in the date is Thu Apr 03 1986 03:00:00 GMT+0300 (Eastern European Summer Time)
Thanks in advance
In the Australia/Sydney time zone, Firefox seems to give an incorrect time for toLocaleString on dates:
new Date("2019-04-04T01:12:38.553309+00:00").toLocaleString() gives "4/4/2019, 11:12:38 AM".
new Date("2019-04-04T01:12:38.553309+00:00").toString() gives correct result "Thu Apr 04 2019 12:12:38 GMT+1100 (AEDT)".
I've been able to reproduce this on my phone as well as my computer. I asked a colleague to test, and Firefox gave the correct output on his computer.
Chromium gives the correct time for toLocaleString.
Is this a bug in Firefox? Do I have some misconfiguration?
I assume the correct approach here is to use a 3rd party library like date-fns to format my Dates instead.
Update: for now I have switched to using dateFns.format(timestamp, 'yyyy/MM/dd HH:mm:ss'). We don't have any US customers anyway.
we are trying to format date in an e-commerce JavaScript application.
We have tried using appending "00:00:00" to the date String.
But in IE 11, with that string we are getting a Invalid Date error.
So we have tried a few more options but the the date Object we are getting is unexpected:
1. new Date(2017, 02, 10, 00, 00, 00, 000) -->
Fri Mar 10 2017 00:00:00 GMT-0800 (Pacific Standard Time)
2. new Date("2017-02-10T00:00:00.000Z") -->
Thu Feb 09 2017 16:00:00 GMT-0800 (Pacific Standard Time)
Is the only effective way is to use "moment.js" to solve these issues.
Thanks and Regards
It is unclear whether you're asking about creating dates or formatting them as strings. But there is nothing unexpected here.
new Date(2017, 02, 10, 00, 00, 00, 000)
You should not use leading zeros on numbers. In some instances in older browsers it can indicate octal rather than decimal. Anyway, the above will produce a "local" date for 10 March 2017 at 00:00:00 based on the host system time zone setting. Yours seems to be set for UTC-08:00.
Given:
new Date("2017-02-10T00:00:00.000Z")
you're asking the Date constructor to parse a string, something it's notoriously bad at. However, browsers older than IE 8 should parse it correctly and produce a date for 10 March 2017 at 00:00:00 UTC, which has a timezone offset of 00:00.
Since your timezone is -08:00, when you write it to output the values will be adjusted for the host timezone (i.e. local) so you see a date for 9 March 2017 at 16:00:00 (or 4 pm).
Note that using the built–in Date.prototype.toString will result in an implementation dependent string that may be different in each host. If you need information on formatting a date, see Where can I find documentation on formatting a date in JavaScript? or How to format a JavaScript date.
Is the only effective way is to use "moment.js" to solve these issues.
It's unclear what issues you're trying to solve, however a good, light, parsing and formatting library is fecha.js. If you also want to do arithmetic, you might find moment.js suitable. But it's not difficult to write a function to parse specific formats or create suitable strings, see the above links.
I am new to DateJs, and the http://www.datejs.com/ is 530 so I can't find documentation for each method.
I need to get the last day of specific year-month. For example :
I need the last fay of July,2015, which should be 31th,July 2015. I tried below script:
<script src="<%=application.getContextPath()%>/js/lib/datejs/date-zh-CN.js"></script>
to=Date.today().set({year:2015,month:6}).final();//which returns Fri Jul 03 2015 00:00:00 GMT+0800 (中国标准时间)
or
to=Date.today().set({year:2015,month:6}).final().day();//which returns Sat Jul 04 2015 00:00:00 GMT+0800 (中国标准时间)
Which are wrong (The day I am ruining the script is at Aug 3rd, 2015). I guess I am using the method final() wrongly, but I can't get a direct example from https://github.com/datejs/Datejs .
Within DateJS there is an moveToLastDayOfMonth function.
Example
Date.july().moveToLastDayOfMonth()
or,
Date.today().set({year:2015,month:6}).moveToLastDayOfMonth()
Hope this helps.
I have never used DateJS, but from a quick study, I guess this is what you'd want:
Date.today().set({year:2015,month:7,day:1}).add(-1).days()
HTH
There's a specific method to do that. First you have to set the date, obviously, and that can be done on an initialized Date object with now() or today(). I'd suggest today as that will zero out the time values:
Date.today().set({year:2015,month:6}).moveToLastDayOfMonth()
This will get you to the exact data you need.
Secondly, if you're using DateJS PLEASE GOD use the latest updated fork (disclaimer, I maintain it, I need to give it a little love but it fixes so many issues with the original library).
You can find that here: https://github.com/abritinthebay/datejs
It's also available as the npm datejs package and in bower. A simple npm install datejs will do if you use NodeJS.
Date.toLocaleString() returns this in Chrome:
Mon Jan 18 2010 16:47:42 GMT+1100 (AUS Eastern Daylight Time)
In firefox, it's this:
Monday, 18 January 2010 4:47:42 PM
Browser version doesn't seem to matter. Anyone have a suggestion on this one?
Date.toLoacaleString is an issue on chrome:
http://code.google.com/p/chromium/issues/detail?id=29779&q=tolocalestring&colspec=ID%20Stars%20Pri%20Area%20Type%20Status%20Summary%20Modified%20Owner%20Mstone%20OS
As CMS answered above, these methods can return pretty much anything they want, hopefully in a representation that is readable to someone in that locale (language + cultural conventions) To obtain a particular formatting of a Date, you need to use a library like DateJS or Dojo.