

Use moment.to, if you want to control the two end points of the interval. This is similar to moment.to, but is special-cased for the current time. This is similar to omNow, but gives the opposite interval: a.fromNow () - a.toNow ().

I found the requestAnimationFrame() pattern to be much a more elegant solution than the setInterval() pattern. This is sometimes called timeago or relative time. requestAnimationFrame() - use this for animation rather than setInterval().Īdditionally here is some light reading about the requestAnimationFrame() pattern:.CountdownJS: (And Rawgit to be able to use countdownjs).An optional parameter can be passed that preserves the current time value and only changes the timezone to UTC. Light reading about the requestAnimationFrame pattern: The moment ().utc () method is used to specify that the given Moment object’s timezone would be displayed as UTC. Var now = moment() // new Date().getTime()
#MOMENT JS NOW CODE#
Note: 2nd line above updates as per momentjs and 3rd line above updates as per countdownjs and all of this is animated at about ~60FPS because of requestAnimationFrame()Īlternatively you can just look at this code snippet: Output: The time is now: 5:29:35 pm, a timer will go off in a minute at 5:30:35 pm $(".countdown").text(countdown(then).toString()) $(".difference > span").text(moment().to(then)) The Javascript: var now = moment() // new Date().getTime() The time is now:, a timer will go off at I created a demonstration here at codepen.io. Var duration = moment.duration(leftTime, 'seconds') Īlthough I'm sure this won't be accepted as the answer to this very old question, I came here looking for a way to do this and this is how I solved the problem. Var leftTime = eventTime - currentTime //Now i am passing the left time from controller itself which handles timezone stuff (UTC), just to simply question i used harcoded values. If we remove the Date constructor fall-back, we can. We could make moment (string) extensible, and then someone could create a plugin that either uses a list of well-known formats or just the Date constructor to parse them. I read the momentjs documentation to figure out the problem, but no luck. If you really want to use the date constructor, you can always just use the date constructor and pass the date into Moment. $('.countdown').text(moment(duration.asMilliseconds()).format('H:mm:ss'))

Var duration = moment.duration(time*1000, 'milliseconds') ĭuration = moment.duration(duration.asMilliseconds() - interval, 'milliseconds') But the time left is not coming as expected. I am calculating date difference between event date and current date (timestamp), then using "duration" method from moment js. I am making a countdown timer for an event page, i used moment js for this.
