Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

Question

Posted

Dear Team,

 

For a virtual device i would like to know how i can get the previous day's date and "previous previous" day's date etc...

 

i know how to get the current date : os.date("%Y%m%d")

but what i need is the day before, and days before. with the same format as 20150511

 

can you please help me

 

thanks you in advance

 

if posted in the wrong topic, please move my post.

4 answers to this question

Recommended Posts

  • 0
Posted

Try: 

 

os.date("%T",os.time()+x*24*3600) 

 

x is the number of days so using 4 is four days from now and -2 is two days ago. The time is in seconds so that's why it's x*24*3600 i.e. 24 hours in a day and 3600 seconds in an hour. So you can use anything you need there just remembering it's seconds (e.g. 1800 for half an hour).

 

That should give you the right result:

 

fibaro:debug(os.date("%Y%m%d")) 
fibaro:debug(os.date("%T"))  
fibaro:debug(os.date("%Y%m%d",os.time()-2*24*3600))
fibaro:debug(os.date("%T",os.time()-2*24*3600))
 
Produces:
 
[DEBUG] 22:58:55: 20150511
[DEBUG] 22:58:55: 22:58:55
[DEBUG] 22:58:55: 20150509
[DEBUG] 22:58:55: 22:58:55
  • 0
  • Inquirer
  • Posted

    Thanks you for your help

     

    if osdate is 20150501

    day-1 is 20150430

     

    do you think that your solution will be ok ?

    • 0
    Posted

    Yes, of course, it is using the date and time functions.

     

    Testing with:

     

    fibaro:debug(os.date("%Y%m%d",os.time()-10*24*3600))
    fibaro:debug(os.date("%Y%m%d",os.time()-11*24*3600))
    fibaro:debug(os.date("%Y%m%d",os.time()-12*24*3600))
    fibaro:debug(os.date("%Y%m%d",os.time()-13*24*3600))
    fibaro:debug(os.date("%Y%m%d",os.time()-14*24*3600))
     
    Gives:
     
    [DEBUG] 14:30:30: 20150502
    [DEBUG] 14:30:30: 20150501
    [DEBUG] 14:30:30: 20150430
    [DEBUG] 14:30:30: 20150429
    [DEBUG] 14:30:30: 20150428
    • 0
  • Inquirer
  • Posted

    thanks you !!

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...