Home > Net >  How can I properly format a date using AngularJS
How can I properly format a date using AngularJS

Time:09-23

I have this line of code that formats a date to MM/DD/YYYY format. My question is what would I pass as a parameter instead of 'MM/DD/YYYY' to get the date to render something like March 17, 2021 instead?

element.find('input').val(moment.tz(ngModel.$viewValue,constants.timezone).format('MM/DD/YYYY'));

CodePudding user response:

You could use formatting options provided by momentjs https://momentjs.com/docs/#/parsing/string-format/.

For your case, you can try this way. Pass your custom date inside the moment()

console.log(moment().format("MMMM DD, YYYY"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

CodePudding user response:

A basic function might help,

$scope.dateFormatChange = function (dateString){
    var allDate = dateString.split(' ');
    var thisDate = allDate[0].split('-');
    var newDate = [thisDate[2],thisDate[1],thisDate[0] ].join("-");
    return newDate;
};

Works both ways, DD-MM-YYYY <-> YYYY-MM-DD

  • Related