In the moment.js
package, the miliseconds()
method returns the number of milliseconds present in a date or time. It can also be used to set the milliseconds of a date or time.
Note: The
milliseconds()
method is the same asmillisecond()
method.
// get millisecondsmoment(datetime).milliseconds()// set millisecondsmoment(datetime).milliseconds(Number)
datetime
: This is the date or time we want to get or set its milliseconds.Number
: This is the number of seconds we want to set our date or time. It can only be from 0 to 999. This method returns a millisecond from 0 to 999. If this range is exceeded, the value returned will be bubbled up to the seconds.
Let's look at the code below:
// require the moment moduleconst moment = require("moment")// create some dates and timeconst date1 = moment(); // current date and timeconst date2 = moment('2020.01.01', 'YYYY.MM.DD')const date3 = moment([2010, 1, 14, 15, 25, 50, 125])const date4 = moment(new Date) // current date and timeconst time1 = moment(1318781876406)const time2 = moment({hour: 20, minute: 20, second: 34})// get the milliseconds of the time and datesconsole.log(date1.milliseconds())console.log(date2.millisecond())console.log(date3.milliseconds())console.log(date4.milliseconds())console.log(time1.millisecond())console.log(time2.milliseconds())
In the code above:
moment
package.moment
package.milliseconds()
method and get the milliseconds of the dates and times created. Let's look at the code below:
// require the moment moduleconst moment = require("moment")// create some dates and timeconst date1 = moment(); // current date and time// get the millisecondsconsole.log(date1.milliseconds())// now set the millsecondsdate1.milliseconds(20)// get the milliseconds againconsole.log(date1.milliseconds())
In the code above:
moment
module.milliseconds()
method to get the milliseconds of the date we created and print it to the console.20
.milliseconds()
method to get the new milliseconds of the date we created, and print the result to the console.