Ejemplo 1: angular obtener la fecha actual aaaa-mm-dd
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');
}
Ejemplo 2: mostrar la marca de tiempo como aaaa mm dd html angular
import { DatePipe } from '@angular/common'
...
constructor(public datepipe: DatePipe){}
...
myFunction(){
this.date=new Date();
let latest_date =this.datepipe.transform(this.date, 'yyyy-MM-dd');
}
Ejemplo 3: obtener angular hoy
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');
Ejemplo 4: cómo mostrar la fecha y hora actuales en angular
//reload the page for evry one minute...put the below code in ngOnInit function
setTimeout(
function(){
location.reload();
}, 60000);
Ejemplo 5: mostrar la marca de tiempo como aaaa mm dd html angular
import { DatePipe } from '@angular/common'
...
providers: [DatePipe]
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)