0

i get a date from a service like "2020-01-08 09:48:34" But how can i transform this with jQuery into "1 day to go" or when the date is "2020-01-15 11:24:34" into "8 days to go"?

I know there are scripts like: How many days until X-Y-Z date? but i am stuck on this date format what i showed above.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Maanstraat
  • 1,241
  • 7
  • 34
  • 63
  • 2
    You can't do this with jQuery. You need JavaScript and/or a different library like Moment.js – VLAZ Jan 07 '20 at 12:00
  • @vlaz how is this possible with only JS? thats fine also by me but i cant use a differtent libary for this. – Maanstraat Jan 07 '20 at 12:02
  • `new Date('2020-01-08 09:48:34')` then see the duplicate I marked – Rory McCrossan Jan 07 '20 at 12:02
  • JavaScript does the date manipulation, while jQuery doesn't have any date functionality aside from `$.now()` which is just a thin wrapper for `(new Date).getTime()`. – VLAZ Jan 07 '20 at 12:05
  • 1
    [How to format time since xxx e.g. “4 minutes ago” similar to Stack Exchange sites](https://stackoverflow.com/questions/3177836) This is for the past ( eg: *"1 days ago"*). You need to implement similarly for the future – adiga Jan 07 '20 at 12:05
  • The safest format to use is actually the abbreviated/simplified ISO8601 format, where the date and time are separated by `T`, even though JS can parse the date propery. This is recommended: `new Date('2020-01-08T09:48:34')` – Terry Jan 07 '20 at 12:06

1 Answers1

0

You can simply do it by getting date difference.

Here, selected_date is your date.

Ideally, you should only use date part for getting your result.

var cur_date = "2020-01-08 12:23:12"
var selected_date = "2020-01-01 10:23:33"
var cur_date_date_part = new Date(cur_date).getFullYear()+ '-'+(new Date(cur_date).getMonth()+1)+'-'+new Date(cur_date).getDate()

var selected_date_date_part = new Date(selected_date).getFullYear()+ '-'+(new Date(selected_date).getMonth()+1)+'-'+new Date(selected_date).getDate()

var resultDays = ((Date.parse(cur_date_date_part) - Date.parse(selected_date_date_part)) / 86400000)
if (resultDays > 0){
  console.log(Math.abs(resultDays) + " days ago")
  }
else{
  console.log(Math.abs(resultDays) + " days to go")
  }
Mayank Patel
  • 1,563
  • 1
  • 14
  • 19
  • This seems to work! And how can i make the value smaller? Like now i get: "0.8548958333333333 days to go" how to translate that to "1 day to go" so i only get full days and not behind the comma. (i have the hours, minutes and seconds also in the date) :-) – Maanstraat Jan 07 '20 at 12:19
  • @Maanstraat use `Math.ceil()` – adiga Jan 07 '20 at 12:31
  • As said by @Maanstraat, either you can use `Math.ceil()` or you can just take date part and calculate as I did in my updated answer. – Mayank Patel Jan 07 '20 at 12:35
  • As mentioned by @VLAZ, you can use Moment.js library which will give you more flexibility with dates. – Mayank Patel Jan 07 '20 at 12:36
  • `new Date(‘2020-01-08 12:23:12’)` returns an invalid Date in at least one current implementation, the method of reduction to the date component is grossly over complicated, jQuery is included for no reason, there is no explanation of how it works. – RobG Jan 08 '20 at 11:39