[JavaScript] format the date object using the prototype attribute

Source: Internet
Author: User

I have always been very familiar with JavaScript, because I will only learn it when it is used in actual projects.
For details about the propertype attribute in JS, refer to W3C. The prototype attribute allows you to add attributes and methods to an object. This is a magical saying. I have also referred to the design mechanism of the js inheritance idea, and it seems that I understand it.
So after a little practice, I found that it was really convenient:

Date. Prototype. toyymmdd = function () {var month = This. getmonth () <10? ("0" + (this. getmonth () + 1) :( this. getmonth () + 1); var day = This. getdate () <10? ("0" + this. getdate (): This. getdate (); return this. getfullyear () + "-" + month + "-" + day ;};

Then use the following code:

VaR now = new date (); var nowstr = now. toyymmdd ();

In this way, you can customize the date format. In fact, write another function similar to toyymmdd (date). Using date as an input parameter for formatting can also achieve the same effect. The simplicity depends on the actual situation.

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.