When I do new Date() in Angular2
(typescript) I GET -> DATEE Wed Jul 18 2018 09:13:40 GMT+0200
The problem is that I have sent Date to Java but Java doesn't let this format.
Java only let ,dd/mm/aaaa or aaaa/mm/dd but he doesn't let Wed Jul 18 2018 09:13:40 GMT+0200.
I try in Angular create a new format but always get String, but I need format Date because my class In Java get Date.
let myDate: Date, myDay, myMonth, myYear;
myDate = new Date();
myDay = myDate.setUTCFullYear;
console.log(myDay);
myDay = myDate.toISOString;
console.log(myDay);
console.log(typeof(myDay));
console.log(new Date(myDay));
I can not install libraries that angular does not bring by default for example: "moment". Ty
In one of my recent projects, I had to deal with multiple custom representations of dates as strings, like YYYY-MM-DD and YYYYMMDD . Since those dates are string variables, TypeScript infers the string type by default.
The international standard recommends writing the date as year, then month, then the day: YYYY-MM-DD.
The dates appear as, mm/dd/yyyy in the U.S. and as, dd/mm/yyyy outside the U.S. where mm is the month, dd is the day, and yyyy is the year. The time is displayed as, hh:mm:ss AM/PM, where hh is the hour, mm is minutes, and ss is seconds.
You can use Angular's DatePipe
. In the component where you want to format:
add
providers: [DatePipe]
then import class
import { DatePipe } from '@angular/common';
then inject in constructor
constructor(public datepipe: DatePipe){}
and then in the code you can format like
this.datepipe.transform(this.dueDate, 'yyyy/MM/dd')
change format from 'yyyy/MM/dd' to any you need
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