get current date with 'yyyy-MM-dd' format in Angular 4

Angular

Angular Problem Overview


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

Angular Solutions


Solution 1 - Angular

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');

Solution 2 - Angular

you can try doing this.


component.ts

currentDate = new Date();

component.html

{{currentDate | date:'yyyy-MM-dd'}}

Solution 3 - Angular

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

curDate=new Date();

<p>{{curDate | date:'yyyy-MM-dd'}}</p>

Solution 4 - Angular

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');

Solution 5 - Angular

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.

Solution 6 - Angular

In Controller ,

 var DateObj = new Date();

 $scope.YourParam = DateObj.getFullYear() + '-' + ('0' + (DateObj.getMonth() + 1)).slice(-2) + '-' + ('0' + DateObj.getDate()).slice(-2);

Solution 7 - Angular

Try this:

   import * as moment from 'moment';

     ngOnInit() {
     this.date = moment().format("YYYY Do MMM");
                }

Solution 8 - Angular

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 

Solution 9 - Angular

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'}
]

Solution 10 - Angular

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;

  }
}

Solution 11 - Angular

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

Solution 12 - Angular

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

Solution 13 - Angular

can use format attribute in HTML.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHazem HASANView Question on Stackoverflow
Solution 1 - AngularkhushView Answer on Stackoverflow
Solution 2 - AngularAbdellah AITMOMINView Answer on Stackoverflow
Solution 3 - AngularKrishna RathoreView Answer on Stackoverflow
Solution 4 - AngularManish PatidarView Answer on Stackoverflow
Solution 5 - AngularPeterView Answer on Stackoverflow
Solution 6 - AngularThisara SubathView Answer on Stackoverflow
Solution 7 - Angularsnehal badheView Answer on Stackoverflow
Solution 8 - AngularSoft Dev Ahmad yar khanView Answer on Stackoverflow
Solution 9 - AngularMeghnath DasView Answer on Stackoverflow
Solution 10 - Angularshehan96View Answer on Stackoverflow
Solution 11 - AngularZiaView Answer on Stackoverflow
Solution 12 - AngularvpapView Answer on Stackoverflow
Solution 13 - AngularYogesh PatilView Answer on Stackoverflow