0

Can I convert to 20200408 these string to Date by TypeScript? If it is yes, then how can?

Sasika Miyuran
  • 135
  • 1
  • 13
  • Please do some research and show the results of that research, and any attempts you made to solve the problem yourself, in the question. That string could be any one of a large number of dates, depending on the format. May 8, 2020? Sep 4, 2020? Sep 20, 2004? – Heretic Monkey May 27 '20 at 18:17
  • Does this answer your question? [Converting a string to a date in JavaScript](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) – Heretic Monkey May 27 '20 at 18:54

3 Answers3

0

If all such strings have the exact same format, you can split them as such (and assuming your format is YYYYMMDD):

let dateStr = '20200408';

let year = dateStr.slice(0,4);
let month = dateStr.slice(4, 6);
let day = dateStr.slice(6, 8);

let date = new Date(year, month, day);
Bucket
  • 7,415
  • 9
  • 35
  • 45
0

Assuming that "20200408" refers to 2020-04-08 in YYYY-MM-DD format:

let full_date_string = "20200408";

let year = Number(full_date_string.substring(0,4));
let month = Number(full_date_string.substring(4,6));
let day = Number(full_date_string.substring(6,8));

let date = new Date(year, month, day);
Aziz Sonawalla
  • 2,482
  • 1
  • 5
  • 6
-2

Below code worked for me.

let dateS = '2020-05-27T00:00:00' 
let dateObj = new Date(dateS);

You can also check below link for conversion in angular.

https://angular.io/api/common/formatDate

vikasyadav53
  • 89
  • 1
  • 2
  • 9