Results 1 to 10 of 10

Thread: HELP: Solution for Posted Date the 15th of the month OR 1st of the following month

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2007
    Posts
    71

    HELP: Solution for Posted Date the 15th of the month OR 1st of the following month

    Hi guys

    could you help what would be the best way to implement this logic

    Example
    If ToDate is May 14 2008 then posted date is 20080515
    and if ToDate is May 31 2008 then posted date is 20080601

    Basicly I would like to create a logic that would work like this
    ToDate = 20000114 THEN PostedDate = 20000115 <-- The 15th of the same month

    ToDate = 20000131 THEN PostedDate = 20000201 <-- The First of the following month

    ToDate = 20001231 THEN PostedDate = 20010101 <-- The year Change (The first of the following month and year)



    Thank you
    Last edited by ndondo; May 1st, 2008 at 04:59 PM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Feb 2007
    Posts
    71

    Re: HELP: Solution for Posted Date the 15th of the month OR 1st of the following month

    and to add more info
    ToDate can be either the 14th of the month or the last day of the month (Can be 28, 30, or 31)

  3. #3
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: HELP: Solution for Posted Date the 15th of the month OR 1st of the following month

    Simple:

    PostedDate = ToDate + 1

    or someone may want to use more "academic" function:

    PostedDate = DateAdd("d", 1, ToDate)

    That will work for any ToDate. PostedDate is always the next day.

    Now, if ToDate is in the form like 20000114 then you need to convert it to Date data type then do adding or use DateSerial function:

    ToDate = 20000114
    PostedDate = CLng(Format(DateSerial(ToDate\10000, (ToDate\100) Mod 100, (ToDate Mod 100) + 1), "yyyymmdd"))
    Last edited by anhn; May 1st, 2008 at 06:56 PM.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2007
    Posts
    71

    Re: HELP: Solution for Posted Date the 15th of the month OR 1st of the following month

    hmm thats simple enough
    thanks..

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: HELP: Solution for Posted Date the 15th of the month OR 1st of the following month

    Does this resolve your issue or do you still have questions?

  6. #6
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: HELP: Solution for Posted Date the 15th of the month OR 1st of the following month

    With all due respect, I do not believe anhn's answer covers the stated problem. Assuming your dates are always in the format "yyyymmdd", code like the following should be used:
    Code:
    Dim ToDate As String
    Dim PostedDate As String
    Dim dtmToDate As Date
    Dim dtmPostedDate As Date
    ' get ToDate into a Date variable:
    dtmToDate = DateSerial(Left$(ToDate, 4), Mid$(ToDate, 5, 2), Right$(ToDate, 2))
    If Day(dtmToDate) >= 15 Then
        ' post first of next month
        dtmPostedDate = DateSerial(CInt(Year(DateAdd("m", 1, dtmToDate))), _
                                            CInt(Month(DateAdd("m", 1, dtmToDate))), 1)
    Else
        ' post 15th of current month
        dtmPostedDate = DateSerial(Year(dtmToDate), Month(dtmToDate), 15)
    End If
    PostedDate = Format$(dtmPostedDate, "yyyymmdd")
    The above code will also handle the year change.
    "It's cold gin time again ..."

    Check out my website here.

  7. #7
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: HELP: Solution for Posted Date the 15th of the month OR 1st of the following mont

    Quote Originally Posted by BruceG
    With all due respect, I do not believe anhn's answer covers the stated problem.
    I don't want to argue. Let the OP test the way I presented.
    Advance 1 day is the simplest process regardless of day, month or year: just add 1 to the given day.
    What the OP want is to have the next day after the 14th day of a month and the next day after the last day of a month.

    Have you ever tried:
    Code:
    ? DateSerial(2008, 2, 31)  '-- 02 Mar 2008 
    ? DateSerial(2008, 4, 31)  '-- 01 May 2008
    ? DateSerial(2007, 12, 35) '-- 04 Jan 2008
    ? DateSerial(2008, 3, 0)   '-- the last day of Feb-2008 (don't care 2008 is a leap year or not)
    Last edited by anhn; May 5th, 2008 at 05:28 PM.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  8. #8
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: HELP: Solution for Posted Date the 15th of the month OR 1st of the following month

    I stand corrected. Your way works if the ToDate is one of the two possibilities he mentioned (the 14th or the last day of the month). With my answer I was considering if days other than those exact two would be input.
    "It's cold gin time again ..."

    Check out my website here.

  9. #9
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: HELP: Solution for Posted Date the 15th of the month OR 1st of the following month

    Quote Originally Posted by BruceG
    I stand corrected. Your way works if the ToDate is one of the two possibilities he mentioned (the 14th or the last day of the month). With my answer I was considering if days other than those exact two would be input.
    Quote Originally Posted by BruceG
    With all due respect, I do not believe anhn's answer covers the stated problem. .
    The code I provided was enough for him to solve his problem as requested and that is "simple enough" (as he commented) with just a single line.
    He didn't ask for a general case of any date and I didn't intend to solve that.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  10. #10
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: HELP: Solution for Posted Date the 15th of the month OR 1st of the following month

    In the end, thread starter will have to check output based on sample data himself... but if this is accounting system related then I would have to agree with BruceG, no sense in posting just transactions before post date and excluding all others.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width