0

I kept dateTime in C# in the form of string

string result =DateTime.Now.ToString("yyyyMMddHHmmss");

The result is: 20201110011515

In ts:

 date:string = 20201110011515

I want to convert it to Date of ts.

new Date(Tue Nov 10 2020 01:15:15 GMT+0200)

Thank you for any help.

1
  • Is there a reason for using "yyyyMMddHHmmss" over "o"? Commented Nov 10, 2020 at 1:21

1 Answer 1

2

Usually dates would get serialized to something like ISO 8601 (or use ToString("o") as John suggests) that will already work with new Date(dateStr). But if you are stuck with this specific format you can take the substring for each part:

let dateStr = "20201110011515";
let year = dateStr.substring(0, 4);
let month = dateStr.substring(4, 6);
let day = dateStr.substring(6, 8);
let hour = dateStr.substring(8, 10);
let minute = dateStr.substring(10, 12);
let second = dateStr.substring(12, 14);

return new Date(year, month, day, hour, minute, second);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.