I have currently set up date formatting on my app..
Html
<span ng-bind="convertToDate(myDate) | date: 'medium'" id="dtText"></span>
Angular
$scope.myDate = new Date();
$scope.convertToDate = function (stringDate) {
var dateOut = new Date(stringDate);
dateOut.setDate(dateOut.getDate());
return dateOut;
};
I have the function working however it is displaying the time which i would like to remove. Just wondering what i would need to add to my function in order to prevent the time from displaying ?
Use the toDateString() method to remove the time from a date, e.g. new Date(date. toDateString()) . The method returns only the date portion of a Date object, so passing the result to the Date() constructor would remove the time from the date. Copied!
The date filter formats a date to a specified format. By default, the format is "MMM d, y" (Jan 5, 2016).
How about just using
<span ng-bind="myDate | date: 'mediumDate'" id="dtText"></span>
No need to convert it.
The reason your convertDate
function doesn't work is that Date.setDate
only sets the date portion of the date value, leaving the time components intact. To reset the time components you would have to reset them individually something like
dateOut.setSeconds(0);
dateOut.setMinutes(0);
dateOut.setHours(0);
Specify format of date as argument
<span ng-bind="myDate | date: 'MMM d,y'" id="dtText"></span>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With