62

How i can get the current date with a specific format 'yyyy-MM-dd', for today by example i with that the result be: '2018-07-12', with using just the command

myDate = new Date();

thanks a lot

Hazem HASAN
  • 1,598
  • 2
  • 21
  • 38

16 Answers16

105

You can use DatePipe for formatting Date in Angular.

In ts if you want to format date then you can inject DatePipe as Service in constructor like this

import { DatePipe } from '@angular/common';

@Component({
    templateUrl: './name.component.html',
    styleUrls: ['./name.component.scss'],
    providers: [DatePipe]
})

myDate = new Date();
constructor(private datePipe: DatePipe){
    this.myDate = this.datePipe.transform(this.myDate, 'yyyy-MM-dd');
}

And if you want to format in html file, 'Shortdate' will return date of type MM/DD/YY

{{myDate | date: 'shortDate' }}

As of Angular 6, this also works,

import {formatDate} from '@angular/common';

formatDate(new Date(), 'yyyy/MM/dd', 'en');
khush
  • 2,702
  • 2
  • 16
  • 35
  • 1
    i imported DatePipe from @angular/common and i applied your code, but i have an error message that format not existing on DateType type? – Hazem HASAN Jul 12 '18 at 07:59
  • 1
    You also need to add DatePipe in providers – khush Jul 12 '18 at 08:30
  • i did that, the error messag is: ERROR in src/app/filter-test/filter-test.component.ts(40,33): error TS2339: Property 'format' does not exist on type 'DatePipe'. – Hazem HASAN Jul 12 '18 at 08:47
  • the only method proposed with this.datePipe is transform? – Hazem HASAN Jul 12 '18 at 08:49
  • 2
    I solved the problem with this commands: test: string; myDate = new Date(); this.test = this.datePipe.transform(this.myDate, 'yyyy-MM-dd'); it works – Hazem HASAN Jul 12 '18 at 08:53
32

you can try doing this.


component.ts

currentDate = new Date();

component.html

{{currentDate | date:'yyyy-MM-dd'}}
davecar21
  • 2,606
  • 21
  • 33
10

You can use date:'yyyy-MM-dd' pipe

curDate=new Date();

<p>{{curDate | date:'yyyy-MM-dd'}}</p>
Krishna Rathore
  • 9,389
  • 5
  • 24
  • 48
7

If you only want to display then you can use date pipe in HTML like this :

Just Date:

your date | date: 'dd MMM yyyy'

Date With Time:

your date | date: 'dd MMM yyyy hh:mm a'

And if you want to take input only date from new Date() then :

You can use angular formatDate() in ts file like this -

currentDate = new Date();

const cValue = formatDate(currentDate, 'yyyy-MM-dd', 'en-US');
Manish Patidar
  • 526
  • 4
  • 14
3

Here is the example:

function MethodName($scope)
{
    $scope.date = new Date();
}

You can change the format in view here we have a code

<div ng-app ng-controller="MethodName">
    My current date is {{date | date:'yyyy-MM-dd'}} . 
</div>

I hope it helps.

Peter
  • 777
  • 2
  • 13
  • 34
2

In Controller ,

 var DateObj = new Date();

 $scope.YourParam = DateObj.getFullYear() + '-' + ('0' + (DateObj.getMonth() + 1)).slice(-2) + '-' + ('0' + DateObj.getDate()).slice(-2);
Thisara Subath
  • 635
  • 6
  • 16
2

app.component.html

<div>
     <h5 style="color:#ffffff;">{{myDate | date:'fullDate'}}</h5>
</div>

app.component.ts

export class AppComponent implements OnInit {
myDate = Date.now();    //date 
David Buck
  • 3,752
  • 35
  • 31
  • 35
  • An answer is much more likely to be useful to people in the future (and get upvotes) if code is accompanied by a short, clear explanation of why your answer solves the problem and, in the case of an old question with 8 other answers, an explanation as to why your answer adds something that was missing from the other answers. – David Buck Mar 06 '20 at 17:44
1

Try this:

   import * as moment from 'moment';

     ngOnInit() {
     this.date = moment().format("YYYY Do MMM");
                }
snehal badhe
  • 102
  • 1
  • 7
1

Use the following:

new Date().toLocaleDateString();

Then as per your requirement just change locale for your application:

import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';
registerLocaleData(localeFr);

//Provide the locale in any Module or Component by the LOCALE_ID token.
providers: [
 {provide: LOCALE_ID, useValue: 'fr'}
]
Meghnath Das
  • 145
  • 1
  • 6
1

To format date in Angular we can use Angular Datepipe.

For an example we can use following code to get formatted date in our code.

import { DatePipe } from '@angular/common';

@Component({
    selector: 'app-name-class',
    templateUrl: './name.component.html',
    styleUrls: ['./name.component.scss']
})

export class NameComponent implements OnInit {
  
  // define datepipe

  datePipe: DatePipe = new DatePipe('en-US');
  
  constructor(){}

  // method to get formatted date

  getformattedDate(){
    
    var date = new Date();
    var transformDate = this.datePipe.transform(date, 'yyyy-MM-dd');
    return transformDate;

  }
}

shehan96
  • 328
  • 1
  • 3
  • 20
1

In case this is to use together w/ <input type="datetime-local"> such as

<input matInput #dateTimeInput type="datetime-local" [value]="todayAsString" step="1" [min]="todayAsString">

then todayAsString should be set as

this.todayAsString = formatDate(new Date(), 'yyyy-MM-dd', 'en');
this.todayAsString += 'T' + formatDate(new Date(), 'hh:mm', 'en');

Documentation: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local

vpap
  • 1,389
  • 2
  • 21
  • 32
1

In HTML FILE

{{date_variable|date:'fullDate'}}

Explanation

Using pipes is better because in pipes we can transform output without changing the input rather than using other methods. Here, I used the following link as fullDate you can use your own combination

enter image description here

Jan
  • 4,974
  • 3
  • 26
  • 43
0

You can use simple var dt = new Date().toISOString().slice(0, 10);

Zia
  • 21
  • 5
0

TS File =>

export class YourComponent implements OnInit {
  //Add This
  myDate = Date.now();

  constructor() { }
  ngOnInit(): void {}
}

HTML File =>

    //Formatted Date.
    <p>{{myDate | date:'yyyy-MM-dd'}}</p>
    //Default date format
    <p>{{myDate | date:'fullDate'}}</p>
Maizied Hasan Majumder
  • 1,197
  • 1
  • 12
  • 25
0

TS(TypeScript)

public currentDate = new Date();

HTML

{{ currentDate | date:'yyyy年MM月dd日' }}

Output:

2022年12月27日
Ram Pukar
  • 1,583
  • 15
  • 17
-1

can use format attribute in HTML.

  • 2
    As an answer, this really needs more detail to actually answer the question. What `format` attribute are you referring to? Can you provide a link to documentation? A code example that demonstrates how to apply it here? Why might this approach be preferred than the accepted answer on this thread? – Jeremy Caney Oct 15 '20 at 21:58