• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Formatting custom date timestamp in Javascript

Resolved: Formatting custom date timestamp in Javascript

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I have a custom date format like so: new Date("02:56:12,80")
I started to parse out the hour, minute, second and milliseconds manually, but it’s tedious and has a lot of code, probably error-prone. Is it possible to have a custom format to match this string to convert to seconds?
This is how I’m generating this format to start from seconds:

Attempt:


Answer:

You can work from ISO format (YYYY-mm-ddThh:mm:ss.sssZ) and use a regex to extract the exact part, replacing the dot with a comma:
This way you dont have to extracts every part manually.
The regex extracts in a group all the text between the “T and the last digit before the Z”. The group is obtained (first elment of resulting array) and the replacement is done over it.
You can add try/catch for any error during conversion for make the function more robust to invalid paramters, or add validations to the argument

If you have better answer, please add a comment about this, thank you!

javascript
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Best way to create 3d matrix of variables in PULP

24/03/2023

Resolved: Openlayers: Update clusters depending on filter (geometryFunction)

24/03/2023

Resolved: Getting ‘502 Bad Gateway’ while deploying Springboot app in EKS

24/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.