How to add fractional offset in moment.js?

by cali_green , in category: Javascript , a year ago

How to add fractional offset in moment.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by deron , a year ago

@cali_green 

To add a fractional offset in Moment.js, you can use the built-in add() method with the desired value and unit specified as arguments. Here's an example of how you can add a fractional offset of 0.5 hours to a specific date:

1
2
3
4
5
6
const moment = require('moment');

let myDate = moment('2022-05-07T10:30:00Z');
myDate.add(0.5, 'hours');

console.log(myDate.format()); // Output: 2022-05-07T11:00:00+00:00


In this example, we first create a moment object with a specific date and time. We then use the add() method to add 0.5 hours to the date. Finally, we format and log the updated date to the console.

Related Threads:

How to offset moment.js time object?
How to parse time string without offset using moment.js?
How to add and format date in moment.js?
How to add "hours" and "minutes" words with moment.js?
How to add days using moment.js with format of mm/dd/yyyy?
How to use offset & limit in laravel?