Need help with Date

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #228059 quote
    kingmordecai
    Participant
    New

    Hello to All,

     

    Date is in the format YYYYMMDD

    is there a way to take a date and algorithmically extract the year, month and day

    Or is there a way to get the number of days between two days?

    When I PRINT(19800220 – 19791215) I get the mathematically correct answer of 8886 but not the number of days

    Also is there a way to algorithmically to convert a date to days?

    It seems all of the temporal functions correspond to the bar number but only for the bars in your history, what I am looking for are ways to calculate these things independent of bar history

     

    Thank you for your Time

    Scott

    #228085 quote
    robertogozzi
    Moderator
    Master

    Here is s snippet to retrieve tha DAY and MONTH https://www.prorealcode.com/topic/tomorrows-date/#post-94733.

    A Date is just an 8-digit number in the format YYYYMMDD.

    To retrieve the DAY simply divide it by 100 truncating the result. Multiply the result by 100 and The DAY is the difference between the result and the original date.

    Use the latter result in the format YYYYMM00 and divide it by 10000, truncating the result. You’ll get YYYY0000. The difference between the two numbets, divided by 100, is the MONTH.

    You can retrieve the YEAR with yhe result YYYY0000 MOD 10000.

    #228086 quote
    robertogozzi
    Moderator
    Master

    Computing a difference between two dates or times is quite complicated and would require several tens of lines.

    #228087 quote
    robertogozzi
    Moderator
    Master

    To truncate a division, use FLOOR(dividend/divisor,0).

    kingmordecai thanked this post
    #228103 quote
    kingmordecai
    Participant
    New

    Thanks Roberto,

    to convert a Date into Year, Month, & Day from what you stating this works

    TheDate = 20240213
    TheYear = Floor(TheDate / 10000, 0)
    TempDay = Floor(TheDate / 100, 0)
    TheDay = ((TheDate / 100) - TempDay) * 100
    
    TempMonth = Floor(TheDate / 100, 0) * 100
    TempMonth2 = TempMonth / 10000
    TheMonth = (TempMonth2 - Floor(TempMonth / 10000, 0)) * 100
    
    PRINT(TheYear)
    PRINT(TheMonth)
    PRINT(TheDay)

    So that is working thank you!

     

    It seems to me that to work with Dates will be very cumbersome in the way that RealTimePro has the Date keyword programmed.

    My main goal is write a function where I pass it two normal dates or Unix Time stamps or excel serial date numbers and it returns the number of days between the two.

    Does anyone have any ideas on how I can go about programming this type of functionality?

    Thank you for your Time

    Scott

    #228104 quote
    kingmordecai
    Participant
    New

    ok I think I will just tackle this by using the number days elapsed from Jan 1st 1900 and keep all of my references to dates in the format of days since that date.

     

    Again thanks to Roberto, for helping me visualize how to tackle my problem!

    robertogozzi thanked this post
    #228150 quote
    robertogozzi
    Moderator
    Master

    You can do some math with seconds between two dates using the timestamp system constant https://www.prorealcode.com/documentation/timestamp/.

    kingmordecai thanked this post
    #228163 quote
    kingmordecai
    Participant
    New

    Hello Roberto,

     

    Thats exactly what I ended up doing. 🙂

    // I pass into the indicator a serial number of the date I am interested in
    // this Offset is positive if the date in question is after Jan 1st, 1970
    // and negative if the date is before Jan 1st, 1970
    Count = Floor(OpenTimestamp/86400,0) - Offset

    This has exactly what I need

    Again thanks for the help and guidance

    Scott

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.

Need help with Date


ProBuilder: Indicators & Custom Tools

New Reply
Author
Summary

This topic contains 7 replies,
has 2 voices, and was last updated by kingmordecai
1 year, 11 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 02/13/2024
Status: Active
Attachments: No files
Logo Logo
Loading...