How do you subtract 1 day from the current time?

24 views
Skip to first unread message

Andy Hurd

unread,
Oct 1, 2012, 12:12:34 PM10/1/12
to Sleep Developers
Hello All,

I have a method that subtracts 1 day from the existing day and returns
the new date.

But I have an issue.

If the day is a Monday then it needs to return the Friday Date, I went
about this the real long way and I know there has to be an easier
way. The way I wrote the method I have to consider if the first of
the month is a Monday then I have to go back to the Friday of the
previous month. With this methodology I have to worry about leap
years.

So there has to be an easier way then the way I am approaching the
problem.

The method would need to do the following:
Tuesday through Friday return the Previous Days date in the form
mmddyyyy
Mondays need to subtract 3 days and return the Friday date in the form
mmddyyyy

Any help would be great.

Andy Hurd

Raphael Mudge

unread,
Oct 1, 2012, 12:16:41 PM10/1/12
to sleep-de...@googlegroups.com
Hi Andy,
Fortunately, this isn't too bad to do. The intermediate form of dates
is a long integer representing the number of milliseconds since an
epoch. Typically, midnight, January 1st, 1970 I think.

I'd use ticks() to get the current day/time. If you want to round it a
little, use &formatDate with MM dd yy to turn ticks() into a value
representing today without the current hour, minute, second, and
milliseconds.

I'd then subtract 24 hours from this.

I'd then use formatDate again to turn the new value into whatever you
need it to look like.

I think Chapter 2 of the Sleep manual covers the date/time formats.
The function reference for parseDate/formatDate will point you to it.

-- Raphael
> --
> -----
> To post to this group, send email to sleep-de...@googlegroups.com
> To unsubscribe from this group, send email to sleep-develope...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sleep-developers?hl=en
>
> The Sleep Scripting Project: http://sleep.dashnine.org/
Reply all
Reply to author
Forward
0 new messages