Results 1 to 3 of 3

Thread: [RESOLVED] How to round a date?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2022
    Posts
    22

    Resolved [RESOLVED] How to round a date?

    Hello friends, I am again with a new question, how can I round a date to a bimester? For example, the bimesters I have is like this:

    - January and February = 01
    - March and April = 03
    - May and June = 05
    - July and August = 07
    - September and October = 09
    - November and December = 11

    So, if my computer says that I am on August 15, 2022, send me to the July and August bimonthly (07), if I am on September 3, give me (09), if I am on December 8, then (11 ). And finally, complement it with the year of the computer.

    I need you to give me the next answer:

    - 202201
    - 202203
    - 202205
    - 202207
    - 202209
    - 202211

    But let the year change, according to the date of the computer. Please help!!

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,849

    Re: How to round a date?

    E.g.
    vb.net Code:
    1. Dim dt = #1/1/2022#
    2.  
    3. Do
    4.     Dim str = $"{dt:d}: {dt.Year}{dt.Month - 1 + (dt.Month Mod 2):00}"
    5.  
    6.     Console.WriteLine(str)
    7.  
    8.     dt = dt.AddMonths(1)
    9. Loop While dt.Year = 2022
    The Mod calculation will produce 1 for odd numbers and 0 for even numbers, so you're basically subtracting 0 from odd numbered months and 1 from even numbered months.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2022
    Posts
    22

    Re: How to round a date?

    Thank you very much, it has worked perfectly for me, I have modified it and this has served me:

    Code:
             Dim dt = Now
             Dim str = $"{dt.Year}{dt.Month - 1 + (dt.Month Mod 2):00}"
             MsgBox(str)

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