Skip to content Skip to sidebar Skip to footer

New Date('dd/mm/yyyy') Instead Of Newdate('mm/dd/yyyy')

Is it possible to enter date in dd/mm/yyyy format into newDate (e.g. 03/01/2018) so that it returns object Wed Jan 03 2018 00:00:00 GMT+0000 (Greenwich Mean Time) {}? If I have a d

Solution 1:

You have no control over the Date constructor, so you need to feed it a date in the format that it wants. Since you are formatting the date yourself, it is better to use the other Date constructor, which takes the year, monthIndex, and day as arguments, since it is more bullet-proof across different browsers and runtimes:

functionmy_date(date_string) {
      var date_components = date_string.split("/");
      var day = date_components[0];
      var month = date_components[1];
      var year = date_components[2];
      returnnewDate(year, month - 1, day);
    }
    
    console.log(my_date("03/01/2018"));

The case of dates one area where I install the moment library on nearly every JavaScript project I create.

Note: Snippet may display the result differently; check your console.

Solution 2:

Solution 3:

I suggest you use momentjs from http://momentjs.com/ . it is very easy to use to output any format u want.

moment().format('MMMM Do YYYY, h:mm:ss a'); // June 28th 2018, 10:30:09 pm
moment().format('dddd');                    // Thursday
moment().format("MMM Do YY");               // Jun 28th 18
moment().format('YYYY [escaped] YYYY');     // 2018 escaped 2018
moment().format();                         

Post a Comment for "New Date('dd/mm/yyyy') Instead Of Newdate('mm/dd/yyyy')"