Okay - I am still very new to JavaScript, please be understanding and patient with me.
I have a script that returns two dates: one from a text file and the other is the system date. These two dates are displayed in this format:
Tue 29 Jun, 12:57 PM (from the text file) and
Mon Jul 26 2021, 11:47:21 PM (from the system date).
When I tried to subtract the two dates, I get this for output on the console:
NaN
I used the following code to try to get the difference between the two dates:
const diffInMilliseconds = Math.abs('computer_date' - 'slug_date');
console.log(diffInMilliseconds);
What does NaN mean? What is the correct way to get the difference of two dates in JavaScript? Do I need to convert the dates to another format before subtracting them?
***** Update ******
I am inserting the code that I have put together for trying to parse the date from a text file and compare to the system date.
const fs = require('fs');
//var str;
var slug_date = " ";
var computer_date = " ";
var diffInMinutes = 0;
fs.readFile('/home/xxxx/xxxx/xxxx/ocrtextparse.txt', 'utf8', function (err, data) {
var targetStr = 'Last Update:';
//var slug = targetStr.split(': ').pop(); // 2020
//var slug = targetStr.substring(targetStr.indexOf(':') + 1); // 01-2020
if (err) throw err;
if(data.includes(targetStr)){
console.log(data)
console.log(targetStr + " is present" + "\n")
//console.log(slug)
}
let arr = data.split(/\r?\n/);
arr.forEach((line, idx)=> {
if(line.includes(targetStr)){
console.log("Line " +(idx+1)+') '+ line + "\n");
//console.log(line.substring(line.indexOf(':')+ 2))
var slug = line.substring(line.indexOf(':')+ 2);
//console.log(slug)
slug_date = slug;
//console.log(slug_date)
}
})
//console.log(slug_date);
// get a new date (locale machine date time)
var date = new Date();
// get the date as a string
var n = date.toDateString();
// get the time as a string
var time = date.toLocaleTimeString();
// log the date in the browser console
//console.log('date:', n);
// log the time in the browser console
//console.log('time:',time);
computer_date = (n + ", " + time);
//console.log(n + ", " + time);
console.log(computer_date);
console.log(slug_date);
//console.log("\n");
var file_date = Date.parse(slug_date);
//const diffInMilliseconds = Math.abs(computer_date - file_date);
//console.log(diffInMilliseconds);
console.log("\n");
//var date1 = new Date(slug_date);
var date2 = new Date(Date.now());
//var date3 = computer_date;
//diffInMinutes = Math.abs(Number(date2.getTime()) - Number(date1.getTime()))/60000;
//console.log(date1)
console.log(date2)
//console.log(date3)
//console.log(diffInMinutes)
var textFileDate = ((slug_date));
var appendYear = textFileDate.replace(',', ' ' + new Date().getFullYear()+',');
//var textFileDate = Date(appendYear);
console.log(computer_date);
console.log(appendYear);
diffInMinutes = Math.abs(Number(date2) - Number(appendYear))/60000;
console.log(diffInMinutes);
console.log(Number(date2));
console.log(Number(appendYear));
var a = new Date(appendYear);
var current = Date.now();
var diff = Math.abs(current - a);
var minutes = Math.floor((diff/1000)/60)
console.log("********")
console.log(minutes);
console.log("++++++++++++");
var dateZ = 'Tue 29 Jun, 12:57:00 PM';
console.log(dateZ);
});
I also have the text file that I am trying to . How can I attach it to my question?