-
Aug 15th, 2022, 11:50 PM
#1
Thread Starter
Junior Member
[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!!
-
Aug 16th, 2022, 12:33 AM
#2
Re: How to round a date?
E.g.
vb.net Code:
Dim dt = #1/1/2022#
Do
Dim str = $"{dt:d}: {dt.Year}{dt.Month - 1 + (dt.Month Mod 2):00}"
Console.WriteLine(str)
dt = dt.AddMonths(1)
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.
-
Aug 16th, 2022, 06:00 PM
#3
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|