Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing time from angular date function

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 ?

like image 549
NewBoy Avatar asked May 11 '16 11:05

NewBoy


People also ask

How do I remove the time from a date object?

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!

What is the date format in angular?

The date filter formats a date to a specified format. By default, the format is "MMM d, y" (Jan 5, 2016).


2 Answers

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);
like image 189
phuzi Avatar answered Sep 28 '22 02:09

phuzi


Specify format of date as argument

<span ng-bind="myDate | date: 'MMM d,y'" id="dtText"></span>
like image 22
Saba Hassan Avatar answered Sep 28 '22 01:09

Saba Hassan