0

I am trying to do some date calculations in javascript but having trouble:

var days = 5;
var init_date = '22-DEC-21';
var result;

today = new Date();
result = today - days;

result I expect to have a date value but it is a numeric value. How can I convert it to date?

Also the variable init_date will need to be converted to date and compared with result. How can this be done in JavaScript?

Coding Duchess
  • 6,445
  • 20
  • 113
  • 209
  • My recommendation would be to use a date library instead of the js Date API directly, unless you plan to study its docs in and out. You can easily achieve what you want with various libs, one [example](https://momentjs.com/docs/#/manipulating/) has a nice API, for your case: `moment().subtract(days, 'days')` and `moment(init_date, "DD-MMM-YY").subtract(days, 'days')`. – Ma3x Jan 28 '22 at 20:46
  • Please spend some time to write your question properly. Variable `init_date` is not used anywhere in your code, "converted to date" seems meaningless with regards to this variable (since it already IS a date), and "to be compared" is also unclear, as there are many different types of comparison. –  Jan 28 '22 at 20:47
  • Does this answer your question? [Add days to JavaScript Date](https://stackoverflow.com/questions/563406/add-days-to-javascript-date) – Mister Jojo Jan 28 '22 at 20:51
  • Mister Jojo, that is exactly what I tried but the result was numeric not date – Coding Duchess Jan 28 '22 at 21:24

2 Answers2

2

To get date minus, say 5 days you can use methods of JS Date() API like this:

let date = new Date();
date.setDate((date.getDate() - 5));

Note: it will return the date in milliseconds, like this:

//1642972791097

To convert it to standard date, use this:

new Date(1642972791097);
//Sun Jan 23 2022 23:19:51 GMT+0200 (Israel Standard Time)

To convert it to local date format, use this:

new Date(1642972791097).toLocaleDateString()
//'1/23/2022' 

To properly parse the string representation like '22-DEC-21' you will need to keep helper object monthsNames. See code example:

let monthsNames = {
    'JAN': 0,
    'FEB': 1,
    'MAR': 2,
    'APR': 3,
    'MAY': 4,
    'JUN': 5,
    'JUN': 6,
    'AUG': 7,
    'SEP': 8,
    'OCT': 9,
    'NOV': 10,
    'DEC': 11
}

let initDate = new Date();
let initDateString = '22-DEC-21';
let initDateArray = initDateString.split('-');
initDate.setDate(initDateArray[0]);
initDate.setMonth(monthsNames[initDateArray[1]]);
initDate.setFullYear(Number('20' + initDateArray[2]));

console.log(initDate);
//Wed Dec 22 2021 23:15:47 GMT+0200 (Israel Standard Time)

Regarding comparing the dates: the easiest way to converts the dates to milliseconds. Say, you have 2 variables keeping the dates in standard format: date1 and date2. So:

let date1Milli = date1.getTime(); // date in milliseconds
let date2Milli = date2.getTime();
console.log(date1Milli > date2Milli);
// true or false 
Cadet
  • 376
  • 1
  • 9
  • I did $dt = date.setDate((date.getDate() - $day)) and the result came out numeric, not date – Coding Duchess Jan 28 '22 at 20:58
  • 1
    You are right. Format to standard Date with the command: new Date(1642972791097) – Cadet Jan 28 '22 at 21:21
  • Thank you! That did give me the value in a valid date format. Now how can I convert my string date variable init_date to a valid date format and compare to the $dt to see which is greater? – Coding Duchess Jan 28 '22 at 21:29
  • I updated the answer with a way of converting the string to Date – Cadet Jan 28 '22 at 21:35
  • Also, updated the answer with a way to compare the dates. – Cadet Jan 28 '22 at 21:43
  • One quick thing on working with javascript dates to keep an eye on when troubleshooting/converting - day/year are 1 based (so february 14, 2016 will have day and year of 14 and 2016) but month is zero based (so february 14, 2016 will have a month of 1). Always trips me up!! – Count Spatula Jan 28 '22 at 22:30
  • Agree, need be careful with it )) – Cadet Jan 28 '22 at 22:59
1

You can use this to begin with:

const daysToSeconds = x => x * 24 * 60 * 60;
const dateToSeconds = x => new Date(x).valueOf() / 1000;
const secondsToDate = x => new Date(x * 1000).toLocaleString().split(",").join("");

const today  = dateToSeconds(new Date());
const days   = daysToSeconds(5);
const result = secondsToDate(today - days);

Other than that, it is not really clear what you want to do with var init_date = '22-DEC-21'.