简体   繁体   中英

Working with TimeStamps and Moment.js

I have a script that updates my database every day at 9:30am. I'm using Ember.js for my application and I need to write a function that checks if a timestamp is past 9:30am or not. The feature should always be showing data, so if it is before 9:30am it should show the previous day's data and if it's after then it should show the current day's data. So essentially the function would return a correct timestamp depending on what time of day it is. I've tried this using moment.js but cannot figure it out. Any help would be great.

    payload.forEach(function(value) {
        // console.log("value: ", value );
        var nineThirty = ' 09:30:00';
        // Create date from input value
        var inputDate = moment( value.updated_at ).format('MM/DD/YYYY');
        var date = moment(inputDate + nineThirty);
        console.log("input Date: ", date );

        // yesterday
        var yesterdayDate = moment().subtract(1, 'days');
        var YD = moment( yesterdayDate ).format('MM/DD/YYYY');
        var yesterday = moment(YD + nineThirty );
        console.log("here: ", yesterday );

        // Get today's date
        var todaysDate = moment().format('MM/DD/YYYY');
        var today_date = moment(todaysDate + nineThirty);

        //... than something
    });

use isBefore this will check if now if 9:30 is before the current time if true date = today's date else the date is yesterdays date

If(moment().set({hour: 9, minute: 30}).isBefore(moment())){
    date = moment().format('MM/DD/YYYY');
}
else{
    date= moment().subtract(1, 'days').format('MM/DD/YYYY');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM