Hi there,
I wanna calculate with the minutes to sleep but obviously run into troubles.
Hi there,
I wanna calculate with the minutes to sleep but obviously run into troubles.
With sleep time, you only have hours and minutes if I recall. while with now() it's both date and time
So when you subtract now from sleep time, you wind up with a screwy value
Also remember the current…
Since sleepTime is expressed in terms of seconds past local midnight, you can determine the current time in terms of seconds past local midnight and compare the two values. Then you can do whatever you…
Hi,
first of all, the forum language is English, so you'll have a better chance to get help when you stick to that.
Secondly, did you read the api reference?
Typical sleep time as configured by the user.
Stored as a Duration past local midnight.
So you should be able to do everything with it that a normal duration also provides.
That's exactly my problem - I can't deal with the duration feature as I want to.
I mean according to the api reference a difference between time.moment and duration should also work but why doesn't this work then?
var now = Time.now();
With sleep time, you only have hours and minutes if I recall. while with now() it's both date and time
So when you subtract now from sleep time, you wind up with a screwy value
Also remember the current time could be 22:17 and the sleep time, 1:32 (the next day)
Ok this explains why that doesn't work.
I another function I'm using
Since sleepTime is expressed in terms of seconds past local midnight, you can determine the current time in terms of seconds past local midnight and compare the two values. Then you can do whatever you want with the result, including converting it to minutes and formatting it for display.
(The docs for Time, Moment and Duration are helpful in this case)
e.g.
// Returns:
// - null if the sleep time is unavailable
// - The number of seconds until today's sleep time:
// >= 0 if the sleep time is now or in the future
// < 0 if the sleep time is in the past
function secondsToSleep() {
var userProfile = UserProfile.getProfile();
if (userProfile.sleepTime == null) {
return null;
}
// get a Moment for the current time
var now = Time.now();
// get a Moment for midnight today (the beginning of the current day)
var today = Time.today();
// current time (in seconds past midnight) = now - today
var currentSecondsPastMidnight = now.compare(today);
return userProfile.sleepTime.value() - currentSecondsPastMidnight;
}
Note that this function returns a negative number if the current time is already past today's sleep time. e.g. If the your sleep time is 10:00 PM (22:00) and the current time is 10:01 PM (22:01), then this function will return -60. It can easily be tweaked to return the time until *tomorrow's* sleep time in this case: if the result is negative, add one day's worth of seconds (24 * 60 * 60) to the result.
Take the current hour and minutes, convert to minutes (min+hour*60)
do the same with sleeptime.
if the sleep mins are less than the now mins add 24*60 mins to sleep mins.(the next day)
subtract now mins from sleep minutes. That gives you minutes until sleep time, which you can then show as hh:mm