1

I have variable oldDate and b. I want to combine them and convert into unix format. oldDate has a day-month-year format. After converting to unix, it seems like it's getting the month as day. Is there a better way to do this or do I need to convert the oldDate to month-day-year format first?

var oldDate = '10-03-2021';
var b = '7:45 AM';
var newB = oldDate + " " + b;
var bDate = new Date(newB).getTime();
console.log(bDate);
output: 1633218300000
tiredqa_18
  • 162
  • 2
  • 9

2 Answers2

1

something like that ?

function date2unix(oldDate, bTime)
  {
  let [d,m,y]   = oldDate.split('-').map(Number)
    , [h,mn,ap] = bTime.split(/:| /).map(s=>Number(s)||(/pm/i.test(s)?12:0))
    ;
  return new Date( y, --m, d, (h % 12) +ap, mn, 0 ).getTime()
  }

sample usage:

function date2unix(oldDate, bTime)
  {
  let [d,m,y]   = oldDate.split('-').map(Number)
    , [h,mn,ap] = bTime.split(/:| /).map(s=>Number(s)||(/pm/i.test(s)?12:0))
    ;
  return new Date( y, --m, d, (h % 12) +ap, mn, 0 ).getTime()
  }

let oldDate = '10-03-2021'
  , b = '7:45 AM'
  ;
let unixDate = date2unix(oldDate, b)
 
console.log('oldDate =', oldDate , `\n`, '     b =', b, `\n ----->` )
console.log('unixDate =', unixDate )



/* -- verif -- */
const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }
let verifDate = new Date(unixDate)
console.log('\nverif:\n', verifDate.toLocaleString(), `\n`, verifDate.toLocaleString('en-US',options) ) 
.as-console-wrapper { max-height: 100% !important; top: 0; }

For info, there is a trap with the AM PM notation :

_ 12am is Midnight ( 00:xx )
_ 12pm is Noon ( 12:xx )

see https://en.wikipedia.org/wiki/12-hour_clock

// on AM -> ap = 0
// on PM -> ap = 12
// ... so :
if (h===12) h = 0 // then h + ap will return the correct hour value        

// or simply (without if)
h %= 12  // in the code :: (h % 12) + ap :: will return the correct hour value 
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
  • There is no need to map to number other than for the hour + 12 part, which can be `+h + 12`. To avoid *toLowerCase* you can do `/pm/i.test(ap)`. But this doesn't deal with 12 am or pm, so you need to `%` the hour first: `(h%12) + (/pm/i.test(ap)? 12 : 0)` since `%` will coerce *h* to number. :-) – RobG Mar 10 '21 at 06:50
  • @RobG thank you for the advice, I only apply it halfway, I just have this preference to keep the map () to simplify the reading of the return – Mister Jojo Mar 10 '21 at 15:07
  • @MisterJojo im not on stackoverflow all the time so i just saw this. code wise looks fine but seems like it's still taking month as date correct? my date is set as dd-mm-yyyy. The time isn't much of a problem. Seems like I had to get the month / date / year separately and combine it again for it to convert to the right date. Thanks for the help though. Appreciate it. – tiredqa_18 Mar 11 '21 at 07:17
  • @tiredqa_18 well, I missed your date format `d-m-y`! I corrected ;) your time format in English made me think your date was also in English format `m-d-y`. Please understand, it's a little frustrating to take time to develop an answer, then to notice the disappearance of the questioner (I have a lot of answers in this same case, some for several months, and some are even really close to my heart, recently this one : https://stackoverflow.com/questions/66099102/javascript-minesweeper-opening-whole-mine-free-area-at-once-not-working-proper/66539667#66539667 ) – Mister Jojo Mar 11 '21 at 15:05
  • @MisterJojo thanks and no worries. awesome way of implementing it – tiredqa_18 Mar 12 '21 at 03:25
0

Didn't really get a better way to do this. Had to get date / month / year separately and put it together again. http://jsfiddle.net/uoghb0v3/

var oldDate = '2021-03-10';
var convertDate = new Date(oldDate);
var newDate = (convertDate.getMonth()+1) + '-' + convertDate.getDate() + '-' + convertDate.getFullYear();
var b = '7:45 AM';
var newB = newDate + " " + b;
var bDate = new Date(newB).getTime();
console.log(bDate);
output: 1615333500000
tiredqa_18
  • 162
  • 2
  • 9