I have a date in this format "Thu Jun 01 00:00:00 CEST 2023". How can I instantiate a Date Obj?
Asked
Active
Viewed 33 times
-4
-
Any number of 'parse this random custom date string to js date' duplicates. Do some research, show what you've tried and what isn't working. SO isn't a coding service. – pilchard May 24 '23 at 12:19
1 Answers
0
You have to create custom function to parse this date string as given below:
function parseCustomDate(dateString){
// Split the date string into individual components
var parts = dateString.split(" ");
// Map the month name to its corresponding index (0-based)
var monthNames = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];
var monthIndex = monthNames.indexOf(parts[1]);
// Extract the day, month, year, and time components
var day = parseInt(parts[2], 10);
var year = parseInt(parts[5], 10);
var timeParts = parts[3].split(":");
var hour = parseInt(timeParts[0], 10);
var minute = parseInt(timeParts[1], 10);
var second = parseInt(timeParts[2], 10);
// Create a new Date object using the extracted components
var dateObj = new Date(year, monthIndex, day, hour, minute, second);
return dateObj;
}
var dateObj = parseCustomDate("Thu Jun 01 00:00:00 CEST 2023");
alert(dateObj);

Manoj Pilania
- 664
- 1
- 7
- 18