Homelab, Linux, JS & ABAP (~˘▾˘)~
 

[JavaScript] Create date object from date string yyyyMMdd

Using regex, you can simply reorder the parts of your date string to create a compatible ISO 8601 date string which is accepted by the new Date() constructor.

const date_string = "20220101"
const oDate = new Date(date_string.replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3'))
console.log(oDate.toUTCString())

Leave a Reply

Your email address will not be published. Required fields are marked *