Home > Software engineering >  javascript I want to do moment subtract or diff
javascript I want to do moment subtract or diff

Time:01-17

I want to subtract the time between event.startDate and nextEvent.startDate.

hello.json

{
    "event": {
        "id": 932,
        "title": "123",
        "startDate": "2023-01-17T00:40:00.000Z",
        "endDate": "2023-01-17T03:29:46.000Z"
    },
    "nextEvent": {
        "id": 930,
        "title": "minsuhi",
        "startDate": "2023-01-18T13:20:00.000Z",
        "endDate": "2023-01-18T16:09:46.000Z"
    }
}

I tried subtraction using diff and tried console.log, but a strange value of 1970-01-02 21:01:00 is output.

If you subtract the two values ​​from each other, the year should come out as 2023 - 2023 = 0000, but 1970 comes out and inquires.

Isn't that what subtracting with diff is? How should I do the calculation?

export default function View() {

    const startTime = moment(hello.data?.event?.startDate);
    const nextStartTime = moment(hello.data?.nextEvent?.startDate);
    
    const duration = nextStartTime.diff(startTime)
    const formattedDuration = moment(duration).format('YYYY-MM-DD HH:MM:SS')
    console.log(formattedDuration) // 1970-01-02 21:01:00

   return (
    
  )
}

CodePudding user response:

moment.diff() gives you the difference between two dates, in milliseconds. The following code snippet:

const startTime = moment(hello.data?.event?.startDate);
const nextStartTime = moment(hello.data?.nextEvent?.startDate);
    
const duration = nextStartTime.diff(startTime)

Gives you the difference in time in milliseconds, but by now converting it to a moment object, you are telling it the equivalent of "duration number of milliseconds after January 1st, 1970" (this date is the default that most computers use as a "time zero").

If you want to get individual differences in time, you'd have to specify individually:

// The false here specifies whether or not you want an integer (false)
// or a floating point number (true)
const durationYears = nextStartTime.diff(startTime, "years", false) 
const durationMonths = nextStartTime.diff(startTime, "months", false) % 12
const durationDays = nextStartTime.diff(startTime, "days", false) % 365
const durationHours = nextStartTime.diff(startTime, "hours", false)
const durationMinutes = nextStartTime.diff(startTime, "minutes", false) % 60
const durationSeconds = nextStartTime.diff(startTime, "seconds", false) % 60

By having these modulos at the end, you force it to stay under it's allotted time. For example, 63 minutes mod 60 is just 3, which is what we want to keep when displaying our numbers. For any time, consider what it carries into, and at what mark (i.e. seconds carry into minutes at 60, months carry into years at 12, etc.). By running the modulo operator on each number, we guarantee it will be less than that number.

CodePudding user response:

You might want to reference the documentation for MomentJS Duration

Getting a duration from moment.diff() should be like this:

const duration =  moment.duration(nextStartTime.diff(startTime))

Also, I'm not quite sure why you are trying to format a duration as a date since it is a length of period, not an exact date.

You could use these to present a duration:

console.log(duration.humanize());     // Prints '2 days' (rounded)
console.log(duration.toISOString());  // Prints 'P1DT12H40M' which is ISO-8601 representation of a duration/period that is 1 day, 12 hours and 40 minutes.

For a full working example:

const startTime = moment("2023-01-17T00:40:00.000Z");
const nextStartTime = moment("2023-01-18T13:20:00.000Z");

const duration =  moment.duration(nextStartTime.diff(startTime))
console.log(duration.humanize());
console.log(duration.toISOString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.1/moment.min.js"></script>

  • Related