1

I have two date format and I need to calculate the hour difference between both.

First date is current date. Second Date is in below format :

var depDate = 25092018  //ddmmyyyy
var depTime = 08:35 //hh:mm

new Date function doesn't take date in format ddmmyyyy. How can input depDate in new Date function

I want to check the difference between current date and depDate.

Can anybody help ?

Mohammad
  • 21,175
  • 15
  • 55
  • 84
Ambika Tewari
  • 251
  • 3
  • 9
  • 2
    Possible duplicate of [Get difference between 2 dates in JavaScript?](https://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript) – Mohammad Oct 04 '18 at 11:37
  • took help from https://stackoverflow.com/questions/13459866/javascript-change-date-into-format-of-dd-mm-yyyy – Ambika Tewari Oct 04 '18 at 11:38
  • But new Date doesn;t take date in format ddmmyyyy. How can input depDate in new Date function – Ambika Tewari Oct 04 '18 at 11:39
  • JavaScript has a `Date` type built in. If you don't need the date time, you should use a [standard format like `yyyy-mm-dd`](https://stackoverflow.com/questions/51715259/what-are-valid-date-time-strings-in-javascript). Please do not use such strange integers (`08:35` is not even valid syntax) instead of proper dates. – str Oct 04 '18 at 11:42
  • If you're going to use integers (they're floats, really..) to describe days (I don't know why you'd do that in JavaScript) at least use the YYYYMMDD format. At least then your integers will be monotonically increasing as time goes on. Have a look at moment.js if you need some advanced date manipulation. – Jochem Kuijpers Oct 04 '18 at 11:45
  • Use `str.replace(/(\d{2})(\d{2})(\d{4})/, "$3-$2-$1")` to formatting date – Mohammad Oct 04 '18 at 11:45
  • Hi @Mohammad I am getting the error Uncaught TypeError: str1.replace is not a function – Ambika Tewari Oct 04 '18 at 11:48
  • `str` is variable contain `25092018` – Mohammad Oct 04 '18 at 11:49
  • new Date accepting mmddyyyy format but not ddmmyyyy. Can i change the str = 25092018 into 09252018 ? – Ambika Tewari Oct 04 '18 at 11:51
  • @AmbikaTewari No, [JavaScript's `Date` constructor does not accept "mmddyyyy"](https://stackoverflow.com/questions/51715259/what-are-valid-date-time-strings-in-javascript). Some browsers might, but don't rely on it. – str Oct 04 '18 at 11:52
  • yeah, so how can I convert 25092018 into 09252018 so that I can input to new Date – Ambika Tewari Oct 04 '18 at 11:53
  • @AmbikaTewari Why would you want to do that? I just wrote that, according to the specification, JavaScript does **not** accept that format. – str Oct 04 '18 at 11:57
  • because page is sending me the date into format ddmmyyyy and I do not have nay control on it. All I can do is to change tje ddmmyyyy into mmddyyyy via js. – Ambika Tewari Oct 04 '18 at 11:59
  • @AmbikaTewari You can change it to whatever you want, not just from one strange format to another strange format. You should use actual date (time) formats as per the spec. Otherwise some browsers might misbehave. – str Oct 04 '18 at 12:00

2 Answers2

1

Your date in variable depDate hasn't valid format. You need to validate format of it using regex in .replace(). Then use Date.prototype.getTime() getting numeric value of date.

var depDate = "25092018";  
var depTime = "08:35";
var dateStr = depDate.replace(/(\d{2})(\d{2})(\d{4})/, "$3-$2-$1")+"T"+depTime;
// dateStr => 2018-09-25T08:35

var timeDiff = Math.abs(new Date(dateStr).getTime() - new Date().getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
// 1000 => to converting millisecond to second
// 3600 => to converting second to hour
// 24 =>   to converting hour to day                    
console.log(diffDays);
Mohammad
  • 21,175
  • 15
  • 55
  • 84
  • Thanks a ton @Mohammed – Ambika Tewari Oct 04 '18 at 12:00
  • This fails in Safari because `'2018-09-25 08:35'` is not a valid date time format. See [What are valid Date Time Strings in JavaScript?](https://stackoverflow.com/questions/51715259/what-are-valid-date-time-strings-in-javascript) – str Oct 04 '18 at 12:02
  • @str Problem fixed – Mohammad Oct 04 '18 at 12:07
  • @Mohammad , depDate is storing the data in format var depDate = 25092018 and not as var depDate = "25092018". So replace is throwing the error as "replace is not a function". how can I convert var depDate = 25092018 into string so that replace works on it. – Ambika Tewari Oct 09 '18 at 06:08
0

If you are using moment module, it would be much easy. Code a below:

let moment = require('moment');
let depDate = "27092018"  //ddmmyyyy
let depTime = "08:35" //hh:mm

let date = moment(depDate, "DDMMYYYY").add(moment(depTime,"HH:mm"))
let difference = moment.duration(moment().diff(date)).asHours();
console.log(difference); // This would be the difference in hours.