2014年5月15日 星期四

javascript day issue


when I want to show the date fetch from backend, I realised that its one day off. ex. the date is 2014/5/15, however it shows 2014/5/14.

The problem is datestring

reference:
http://stackoverflow.com/questions/9509360/datepicker-date-off-by-one-day

console.log(new Date('2012-03-21')); //prints Tue Mar 20 2012 20:00:00 GMT-0400 (Eastern Daylight Time)
The Javascript Date object can accept one of the following syntax as below,
  1. new Date()
  2. new Date(milliseconds)
  3. new Date(dateString)
  4. new Date(year, month, day [, hour, minute, second, millisecond ])
So in your case it is going to call the dateString and parse. So try appending the time as below,
new Date ('2012-03-21T00:00:00') //should return you Wed Mar 21 2012
or Better to use as below,
new Date (2012, 2, 21). 
year - Integer value representing the year. For compatibility (in order to avoid the Y2K problem), you should always specify the year in full; use 1998, rather than 98.
month - Integer value representing the month, beginning with 0 for January to 11 for December.
day - Integer value representing the day of the month (1-31).



沒有留言:

張貼留言