Converting the dd.mm.yyyy format to yyyy-mm-dd

How to convert dd.mm.yyyy format date to yyyy-mm-dd format in JavaScript?

Here is an example:

30.01.2010 to 2010-01-30 

The value converts dmY to Ymd. I know how to do this in PHP, but I need it in JavaScript.

+7
source share
5 answers

Datejs can make it out. Code at http://datejs.googlecode.com/files/date.js

UPDATE: it is not safe to leave date.js to determine the format string automatically. I made a mistake by not testing with & lt; = 12 (spirit). You should use:

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

or

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

depending on whether you want to allow clear days.

+8
source

You can do this quite simply. Just divide the European date into an array, cancel it and then attach a dash to it.

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

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

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

For such conversions that do not require any date logic, it is usually wise to use string manipulation tools.

+7
source

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

This information is available on the Mozilla Development Network (MDN): Intl.DateTimeFormat MDN Web Documents

You may have a date in the form of a DateString, for example: "Thu October 3, 2019." Having this, we can convert it to the format we need, but first we need to convert this string into an object containing information about the date:

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

Then we can convert the date to the format we need:

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

Change "en-GB" to "en-US", if you live in the United States, look for other formats, if you do not need them, you can always see the codes of other languages ​​here:

Locale Language Codes

+2
source
  function stringToDate( 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 = new Date( value ) } if ( isNaN( date.getTime() ) || !isIso ) { return false } 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) 
+1
source

All Articles