Skip to content Skip to sidebar Skip to footer

Convert Dd.mm.yyyy Format To Yyyy-mm-dd

How can I convert dd.mm.yyyy format date to yyyy-mm-dd format in JavaScript? Here is an example: 30.01.2010 to 2010-01-30 Meaning convert d.m.Y to Y-m-d. I know how to do this in

Solution 1:

You can do this pretty simply. Just split the european date into an array, reverse it, and then join it with dashes.

var euro_date = '30.01.2010';
euro_date = euro_date.split('.');
var us_date = euro_date.reverse().join('-');

Solution 2:

Datejs can parse that. The code is at http://datejs.googlecode.com/files/date.js

EDIT: It is not safe to left date.js determine the format string automatically. I made the mistake of not testing with a day <= 12 (duh). You should use:

Date.parseExact('09.01.2010', 'd.M.yyyy').toString('yyyy-MM-dd');

or

Date.parseExact('09.01.2010', 'dd.MM.yyyy').toString('yyyy-MM-dd');

depending on whether you want to allow single digit days.

Solution 3:

Datejs is a bit bloated if you only need to do this. You can use split() and concatenate the results:

var eu_date = '30.01.2010';
var parts = eu_date.split('.');
var us_date = parts[2]+'-'+parts[1]+'-'+parts[0];

For these kinds of conversions where no date logic is needed, it's usually smartest to just use string manipulation tools.

Solution 4:

const date = newDate(Date.UTC(2012, 11, 20, 3, 0, 0));
// Results below assume UTC timezone - your results may varyconsole.log(newIntl.DateTimeFormat('en-US').format(date));
// expected output: "12/20/2012"console.log(newIntl.DateTimeFormat('en-GB').format(date));
// expected output: "20/12/2012"// Include a fallback language, in this case Indonesianconsole.log(newIntl.DateTimeFormat(['ban', 'id']).format(date));
// expected output: "20/12/2012"

This information is hosted in the Mozilla Development Network (MDN): Intl.DateTimeFormat MDN Web docs

You can have a date in the form of DateString, for example: "Thu Oct 03 2019". Having this we can convert it in the format we would like, but first we have to convert this string to an object containing the information of the date:

let convertedDate = new Date("Thu Oct 03 2019")

Then we can convert the date in the format that we would like to have it:

let FinalDate = new Intl.DateTimeFormat('en-GB').format(convertedDate)

Change "en-GB" to "en-US" if you live in the United States, search for other formats if these are not what you need you can always look for other language codes here:

Language locale codes

Solution 5:

functionstringToDate( value ) {
      var isEuro = value.match( /^\d{1,2}\.\d{1,2}\.\d{4}$/ )
      var isIso = value.match( /^\d{4}-\d{1,2}-\d{1,2}$/ )

      if ( isEuro ) {
        value = value.split('.')
        value = [value[2],value[1],value[0]].join('-') //.reverse() isn't working in IEdge
        isIso = true
      }

      if ( isEuro || isIso ) {
        var date = newDate( value )
      }

      if ( isNaN( date.getTime() ) || !isIso ) {
         returnfalse
      }


      return date
    }

    stringToDate('30.01.2000') //Sun Jan 30 2000 00:00:00 GMT+0100 (CET)stringToDate('30.1.2000') //Sun Jan 30 2000 01:00:00 GMT+0100 (CET)stringToDate('2000-01-30') //Sun Jan 30 2000 01:00:00 GMT+0100 (CET)stringToDate('2000-1-30') //Sun Jan 30 2000 01:00:00 GMT+0100 (CET)

Post a Comment for "Convert Dd.mm.yyyy Format To Yyyy-mm-dd"