MOCKSTACKS
EN
Questions And Answers

More Tutorials









Javascript Formatting date

In modern browsers (*), date.prototype.toLocaleDateString() allows you to define the formatting of a Date in a convenient manner.

It requires the following format :


dateObj.toLocaleDateString([locales [, options]])

The locales parameter should be a string with a BCP 47 language tag, or an array of such strings.
The options parameter should be an object with some or all of the following properties:


  • localeMatcher : possible values are "lookup" and "best fit"; the default is "best fit".

  • timeZone : the only value implementations must recognize is "UTC"; the default is the runtime's default time zone.

  • hour12 :possible values are true and false; the default is locale dependent.

  • formatMatcher : possible values are "basic" and "best fit"; the default is "best fit".

  • weekday : possible values are "narrow", "short" & "long".

  • era : possible values are "narrow", "short" & "long".

  • year : possible values are "numeric" & "2-digit".

  • month : possible values are "numeric", "2-digit", "narrow", "short" & "long".

  • day : possible values are "numeric" & "2-digit".

  • hour : possible values are "numeric" & "2-digit".

  • minute : possible values are "numeric" & "2-digit".

  • second : possible values are "numeric" & "2-digit".

  • timeZoneName : possible values are "short" & "long".

  • var today = new Date().toLocaleDateString('en-GB', {
     day : 'numeric',
     month : 'short',
     year : 'numeric'
    });
    

    Output


    Going custom


    If Date.prototype.toLocaleDateString() isn't flexible enough to fulfill whatever need you may have, you might
    want to consider creating a custom Date object that looks like this


    var DateObject = (function() {
     var monthNames = [
     "January", "February", "March",
     "April", "May", "June", "July",
     "August", "September", "October",
     "November", "December"
     ];
     var date = function(str) {
     this.set(str);
     };
     date.prototype = {
     set : function(str) {
     var dateDef = str ? new Date(str) : new Date();
     this.day = dateDef.getDate();
     this.dayPadded = (this.day < 10) ? ("0" + this.day) : "" + this.day;
     this.month = dateDef.getMonth() + 1;
     this.monthPadded = (this.month < 10) ? ("0" + this.month) : "" + this.month;
     this.monthName = monthNames[this.month - 1];
     this.year = dateDef.getFullYear();
     },
     get : function(properties, separator) {
     var separator = separator ? separator : '-'
     ret = [];
     for(var i in properties) {
     ret.push(this[properties[i]]);
     }
     return ret.join(separator)
    }
    };
     return date;
    })();


    To get a formatted string, you could do something like this


    new DateObject().get(['dayPadded', 'monthPadded', 'year'])

    Output



    Conclusion

    In this page (written and validated by ) you learned about Javascript Formatting date . What's Next? If you are interested in completing Javascript tutorial, your next topic will be learning about: Javascript current time and date.



    Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


    Share On:


    Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.