-1

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

Cool Tech
  • 7
  • 2
  • 1
    Does this answer your question? [How to format a JavaScript date?](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – LeeLenalee Aug 29 '21 at 01:50
  • i try but no - this is what i got $('.timepicker-no-seconds').timepicker({ autoclose: true, timeFormat: 'hh:mm:ss', minuteStep: 1, defaultTime: false }); – Cool Tech Aug 29 '21 at 01:52
  • what does `$('.timepicker-no-seconds'). .....` have to do with formatting the date in that link? – Bravo Aug 29 '21 at 02:55
  • `.replace(/\s([1-9])/, ' 0$1')` – Bravo Aug 29 '21 at 03:12
  • Please provide enough code so others can better understand or reproduce the problem. – Community Aug 31 '21 at 08:25

1 Answers1

0

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
LeeLenalee
  • 27,463
  • 6
  • 45
  • 69
mti2935
  • 11,465
  • 3
  • 29
  • 33