moment.js get current time in milliseconds?

前端 未结 7 1250
旧巷少年郎
旧巷少年郎 2021-02-01 11:54
var timeArr = moment().format(\'HH:mm:ss\').split(\':\');

var timeInMilliseconds = (timeArr[0] * 3600000) + (timeArr[1] * 60000);

This solution works,

7条回答
  •  無奈伤痛
    2021-02-01 12:12

    Since this thread is the first one from Google I found, one accurate and lazy way I found is :

    const momentObject = moment().toObject();
    // date doesn't exist with duration, but day does so use it instead
    //   -1 because moment start from date 1, but a duration start from 0
    const durationCompatibleObject = { ... momentObject, day: momentObject.date - 1 };
    delete durationCompatibleObject.date;
    
    const yourDuration = moment.duration(durationCompatibleObject);
    // yourDuration.asMilliseconds()
    

    now just add some prototypes (such as toDuration()) / .asMilliseconds() into moment and you can easily switch to milliseconds() or whatever !

提交回复
热议问题