Conversion between a Date object and a string in JavaScript

Source: Internet
Author: User

Using the data object from the new date () in JS to convert to a string often fails to meet the expectations we want, and today I'm going to make a simple analysis of the topic "Conversion between Date objects and strings":

Common ways to convert are:

One, toString ()--Converts the Date object to a string

1 var New Date (). toString (); 2     Console.log (str); 3 4 // Output: 5 // Mon Oct 11:13:22 gmt+0800

Second, totimestring ()--Converts the time portion of a Date object to a string

1 var New Date (). totimestring (); 2     Console.log (str); 3 // Output: 4 // 11:16:31 gmt+0800

Three, todatestring ()--Converts the date part of a Date object to a string

1 var New Date (). todatestring (); 2     Console.log (str); 3 // Output: 4 // Mon Oct

It is obvious that the conversion results from the above three methods do not meet our expectations and require further processing before the Date Object can be converted to a time string that is easy to display or interact with the background , as follows:

To define a function:

1 //To define converting a Date object to a string function2 functiontimetostring (timeobj) {3     varstr = "";4     varYear =timeobj.getfullyear ();5     varmonth =timeobj.getmonth ();6     varDate =timeobj.getdate ();7     varTime = Timeobj.totimestring (). Split ("") [0];8     varRex =NewREGEXP (/:/g);9str = year+ "-" +month+ "-" +date+ "" +time.replace (Rex, "-");TenConsole.log ("------Current date:" +str); One     returnstr; A } -  - //to define a conversion to a string function by timestamp the functionTimetoobj (mstime) { -     varD =NewDate (); - D.settime (mstime); - timetostring (d); +}

Execute function:

1 //To define converting a Date object to a string function2 varD =NewDate ();3 timetostring (d);4 //Output:5 //------Current Date: 2015-9-12 11-43-286 7 //to define a conversion to a string function by timestamp8Timetoobj (1444617383284);9 //Output:Ten //------Current Date: 2015-9-12 10-36-23

You can refer to the Date object API:

Shcool JavaScript Date Object http://www.w3school.com.cn/jsref/jsref_obj_date.asp

Summarize:

This analysis process is not only a practical application of my summary, but also for the browser to provide a solution to this kind of related problems, is purely a point of view, I hope that the browser can be more in-depth analysis, and write a more complete system of conversion rules.

Conversion between a Date object and a string in JavaScript

Related Article

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.