Convert dd.mm.yyyy format to yyyy-mm-dd

后端 未结 5 1481
孤街浪徒
孤街浪徒 2020-12-16 05:24

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

相关标签:
5条回答
  • 2020-12-16 05:38
        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)
    
    0 讨论(0)
  • 2020-12-16 05:42

    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.

    0 讨论(0)
  • 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('-');
    
    0 讨论(0)
  • 2020-12-16 05:51

    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.

    0 讨论(0)
  • 2020-12-16 05:53

    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 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

    0 讨论(0)
提交回复
热议问题