Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I format Date in typescript?

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

like image 886
EduBw Avatar asked Jul 18 '18 07:07

EduBw


People also ask

Is date a string in TypeScript?

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.

How do you format a date?

The international standard recommends writing the date as year, then month, then the day: YYYY-MM-DD.

How do I display a date in a specific format?

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.


1 Answers

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

like image 77
Woworks Avatar answered Oct 20 '22 09:10

Woworks