B
B
BonBon Slick2017-01-10 20:40:17
JavaScript
BonBon Slick, 2017-01-10 20:40:17

How to calculate date difference in seconds?

The current date, and the date that came from the database:

<script type="text/javascript">
    var userDate = new Date();
    var currentDateTime = userDate.getFullYear() + "-" + "0" + (userDate.getMonth() + 1) + "-" + userDate.getDate() + " " + userDate.getHours() + ":" + userDate.getMinutes() + ":" + userDate.getSeconds();
    var endDate = "<?php echo $end_date;?>"; // это string 
</script>

I made a crutch, on the server I split the date from the database with a regular program, and on the front I do this:
var userDate = new Date();

    var currYear =  userDate.getFullYear();
    var currMonth = "0" + (userDate.getMonth() + 1);
    var currDay =  userDate.getDate();
    var currHours =  userDate.getHours();
    var currMinutes =  userDate.getMinutes();
    var currSec =  userDate.getSeconds();

    var endYear = "<?php echo $year;?>";
    var endMonth = "<?php echo $month;?>";
    var endDay = "<?php echo $day;?>";
    var endHour = "<?php echo $hour;?>";
    var endMinute = "<?php echo $minute;?>";
    var endSec = "<?php echo $second;?>";

    var yearDiff = endYear - currYear;
    var monthDiff = endMonth - currMonth;
    var dayDiff = endDay - currDay;
    var hourDiff = endHour - currHours;
    var minDiff = endMinute - currMinutes;
    var secDiff = endSec - currSec;

    var yearDiffInSec = yearDiff * 365 * 24 * 60 * 60;
    var monthDiffInSec = monthDiff * 30 * 24 * 60 * 60;
    var dayDiffInSec = dayDiff * 24 * 60 * 60;
    var hourDiffInSec = hourDiff * 60 * 60;
    var minDiffInSec = minDiff * 60;

    var diffInSec = secDiff + minDiffInSec + hourDiffInSec + dayDiffInSec + monthDiffInSec + yearDiffInSec;

I think there is an easier solution :)
Somehow subtract the current date from the end date and get the difference in seconds

Answer the question

In order to leave comments, you need to log in

6 answer(s)
S
Stepanya, 2017-01-10
@BonBonSlick

var differenceSeconds = ((new Date() -  new Date("<?php echo $timestamp;?>")) / 1000).toFixed(0);

A
Alexander Aksentiev, 2017-01-10
@Sanasol

"0" + (userDate.getMonth() + 1) + "
good luck with 010 and 011 months xD
The date comparison algorithm is always the same.
Convert dates to the same format and compare.
So unix timestamp in hand and go.

D
DimaIs, 2017-01-10
@DimaIs

Отнять год от года, месяц от месяца, день ото дня, потом создать переменную, равную разница годов*ково секунд в году + разница месяцей* секунд в месяце + разница дней* секунд в 1 дне. Проблема в том, что в месяце не равное колво дней, но задумку ты понял:)

V
vintage, 2017-01-10
@vintage

https://habrahabr.ru/post/263041/

R
RidgeA, 2017-01-10
@RidgeA

What prevents you from using https://developer.mozilla.org/ru/docs/Web/JavaScri... ?
The result will only need to be divided by 1000.
Well, there is still https://momentjs.com/ .

E
EVG, 2017-01-10
@f_ban

Here is a great library for working with dates. I use both on the server (NodeJS) and on the client.
https://momentjs.com

var currMoment = moment();
var dbMoment = moment(dbTime);

var diffSec = currMoment.diff(dbMoment, 'seconds');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question