Java Date and javascript Date, datejavascript
I recently wrote a page showing the date. Generate a Date in Java, and then send the Date to the vm template through velocity.
The Code is as follows:
var dates = new Date("$!{pp.date}");var dateStr = date2String(dates);
Here, date2String is like this. For function reference
Http://www.w3school.com.cn/jsref/jsref_obj_date.asp
function date2String(date) {var year = date.getFullYear();var month = date.getMonth();var day = date.getDate();var hour = date.getHours();var minute = date.getMinutes();var second = date.getSeconds();var monthStr = (month <= 9 ? "0":"") + month;var dayStr = (day <= 9 ? "0":"") + day;var hourStr = (hour <= 9 ? "0":"") + hour;var minuteStr = (minute <= 9 ? "0":"") + minute;var secondStr = (second <= 9 ? "0":"") + second;return ""+year+"-"+monthStr+"-"+dayStr+" "+hourStr+":"+minuteStr+":"+secondStr;}
Results parsing found that time was too messy!
First, the month is incorrect. This is a careless bug. Because the getMonth result is 0-11, you need to add more than 1 in the future. This is easier to solve.
The latter is not deformed due to less than 1.
There are two solutions: one is to directly pass a Java modified format string to js, and the other is to think about other methods.
If you want to be lazy, use it. But if you think about it, you can still be lazy and learn new things. Use it.
If you want to pass the Date object directly, the implementation may be quite strange. Date should be the time difference from January 1, 1970. Therefore, there should be a parameter using a long constructor. Of course, the above
Only one constructor method is provided on the Chinese w3c webpage, that is, the current time is obtained.
There is no problem with directly assigning values to date, so I think there should be another constructor. So I can find it again and check the constructor of Date:
Http://www.tutorialspoint.com/javascript/javascript_date_object.htm
Use
new Date(milliseconds)
Reliable, after all, it is stored in the underlying data.
Therefore, change:
var dates = new Date($!{pp.date.getTime()});
Reliable. Let's take a look at why it was so unreliable.
The Date passed in is:
Date = Sun Dec 14 09:40:11 CST 2014
Date = Sat Dec 13 23:54:00 CST 2014
However:
Changed to 23:40:11
And 13:54:00
They all increased by 14 hours!
After printing, we found that:
Dates = Sun Dec 14 2014 13:54:00 GMT + 0800
Dates = Sun Dec 14 2014 23:40:11 GMT + 0800
This is the time zone problem!
According to the CST above on the Wiki:
Http://zh.wikipedia.org/wiki/CST
In addition to China, it can also represent Australia and Central America. It should be the central United States time is relatively reliable, we are in the east 8 area, then the central United States should be in the West 6 area.
Check the result.
Therefore, js regards CST as Central Standard Time (North America) rather than China Standard Time ).
This leads to corresponding errors. After a while, you can still find the cause. Haha. Come on.
======
PS. Be careful. You must discover the problem and remedy it if the problem persists. Be careful when testing.