i have a value of "08-28-2021 1:00:00 pm" is in $('#strDinnerStartTime').val)
i need to format it "08-28-2021 01:00:00 pm" where the hour is not just 1 but 01.
how can i do this thank you
i have a value of "08-28-2021 1:00:00 pm" is in $('#strDinnerStartTime').val)
i need to format it "08-28-2021 01:00:00 pm" where the hour is not just 1 but 01.
how can i do this thank you
The format that your date/time string is in is not recognized by javascript's built-in Date.parse()
function. So, you may need to fallback to 'disassembling' the date/time string, formatting the hours with leading-zero padding, then 'reassembling' the date/time string, like so:
var s = "08-28-2021 1:00:00 pm";
var datepart = s.split(' ')[0];
var timepart1 = s.split(' ')[1];
var timepart2 = s.split(' ')[2];
var hours = timepart1.split(':')[0];
var minutes = timepart1.split(':')[1];
var seconds = timepart1.split(':')[2];
var hoursstr = String(hours).padStart(2, '0');
var minutesstr = String(minutes).padStart(2, '0');
var secondsstr = String(seconds).padStart(2, '0');
var result = datepart + ' ' + hoursstr + ':' + minutesstr + ':' + secondsstr + ' ' + timepart2;
console.log(result); //produces 08-28-2021 01:00:00 pm