javascriptangularjsangular-date-format

Display date in localized format in AngularJS


I have a scenario in which I have to set a date for event, the date picker should take the date format in localized format according to country or region. How can I do that in AngularJS? I have tried many solutions but didn't find any appropriate one.


Solution

  • You can use toLocaleDateString()

    Reference

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

    var today = new Date();
    
    var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
    
     console.log(date.toLocaleDateString('en-US'));
     console.log(today.toLocaleDateString('en-US'));