Java Date and JavaScript date

Source: Internet
Author: User

Recently wrote a page, above to show the next date. A date is generated in Java, and the date is fed into the VM template by velocity

The code is as follows:

var dates = new Date ("$!{ Pp.date} "); var datestr = date2string (dates);

Where date2string is this, the 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;}

Result analysis found time is all messed up!

First of all, the month is wrong, this is a careless bug, because getmonth get the result is 0-11, so need in the back +1, this relatively easy to fix

The back is not deformed by the reason of less than 1.

Now there are two kinds of scenarios: one, directly pass a Java modified good format string to JS, and second think of other ways.

If you want to be lazy, use one. But think to think or second more can lazy, but also can learn new things, with the second bar.


Thinking about passing the date object directly might actually be a bit tricky to implement. Date should be the time difference from the beginning of 1970, so the parameters of a long constructor should be there. Of course, on top.

The Chinese web site only gives a construction method, that is, to get the current time.


Even the previous direct assignment date is nothing, so I think there should be other constructors, so choose to look at the constructor for the following date:

Http://www.tutorialspoint.com/javascript/javascript_date_object.htm


Use

New Date (milliseconds)

A little bit, after all, is stored in the underlying data.

So change to:

var dates = new Date ($!{ Pp.date.getTime ()});

More reliable. Let's see why the original is so unreliable.

The original date passed in is:

Date=sun Dec 09:40:11 CST 2014
DATE=SAT Dec 23:54:00 CST 2014

But:

It becomes 2014-12-14 23:40:11

and 2014-12-14 13:54:00

It's been an increase of 14 hours!

After printing found:

Dates=sun Dec 13:54:00 gmt+0800

Dates=sun Dec 23:40:11 gmt+0800

So that's the time zone problem!

According to the CST above the Wiki:


Http://zh.wikipedia.org/wiki/CST


In addition to representing China, it can also represent time in Australia and Central America. It should be Central America time is more reliable, we are in the East 8 district, then the central United States should be in the West 6 district.

See for sure.


So JS is the CST as the central time of the United States (NorthAmerica) rather than (China standardtimes).

resulting in a corresponding error. Toss a bit or can find the reason haha. Come on.


======

Ps. Still have to be careful ah, can not old problem to find out problems and then to remedy. You must be careful when you test.







Java Date and JavaScript date

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.