user14860979
user14860979

Reputation: 127

Convert Array to Date yyyy-mm-dd

with typescript I'm doing the next:

 var today = new Date();
   var startDate = today;
   startDate.setDate(today.getDate() - 7);
   var StartDate = startDate.toISOString().split('T')[0];
   var endDate = new Date();
   endDate.setDate(today.getDate() + 7);
   var EndDate = endDate.toISOString().split('T')[0];

I'm doing this by this way because I need a format with yyyy-mm-dd but also I need it in "Date" type, but it is returning me an array but I don't know how to change it into an Date.

In this case EndDate and StartDate are the finish vars that I'm gonna use.

Upvotes: 0

Views: 307

Answers (1)

Syed Mohib Uddin
Syed Mohib Uddin

Reputation: 716

You can try it like this Playground link

function join(t: Date, a: any, s: string) {
   function format(m: any) {
      let f = new Intl.DateTimeFormat('en', m);
      return f.format(t);
   }
   return a.map(format).join(s);
}

let a = [{year: 'numeric'}, {month: 'short'}, {day: 'numeric'}];
let s = join(new Date, a, '-');
console.log(s);

Upvotes: 1

Related Questions