Solved Calculate the last date of each current year (1 Viewer)

gstylianou

Registered User.
Local time
Today, 13:24
Joined
Dec 16, 2013
Messages
357
Good Evening,

I'm trying to figure out how to automatically calculate the last date of each current year. For example if we are in 2023 I should find the result 31/12/2023 or if we are in 2024 the result 31/12/2024 (the same result coincides)
Any idea?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 05:24
Joined
Feb 28, 2001
Messages
27,186
dtLastDate = CDate( "#31/12/" & DatePart( "yyyy", Now() ) & "#" )
 

ebs17

Well-known member
Local time
Today, 12:24
Joined
Feb 7, 2020
Messages
1,946
Code:
dtLastDate = DateSerial(Year(Date(), 12, 31)
 

gstylianou

Registered User.
Local time
Today, 13:24
Joined
Dec 16, 2013
Messages
357
Code:
dtLastDate = DateSerial(Year(Date(), 12, 31)
Hi and thnaks for your help but,

im getting error when o run the code as following

Dim dtLastDate As Date
dtLastDate = DateSerial(Year(Date, 12, 31))
MsgBox dtLastDate
 

Attachments

  • Capture.JPG
    Capture.JPG
    23.4 KB · Views: 54

gstylianou

Registered User.
Local time
Today, 13:24
Joined
Dec 16, 2013
Messages
357
dtLastDate = CDate( "#31/12/" & DatePart( "yyyy", Now() ) & "#" )
Hi, thanks,

Maybe i made something wrong ?

Dim dtLastDate As Date
dtLastDate = CDate("#31/12/" & DatePart("yyyy", Now()) & "#")
MsgBox dtLastDate
 

Attachments

  • Capture.JPG
    Capture.JPG
    18.9 KB · Views: 48

ebs17

Well-known member
Local time
Today, 12:24
Joined
Feb 7, 2020
Messages
1,946
getting error when o run the code as following
Clear. You should copy correctly. You can then check your own creations separately.

As for the Doc's suggestion: CDate can only convert certain string formats to a date. The string formats that can be used depend on your own language and country settings. The ISO format should always work:
Code:
? CDate("2023-12-31")
31.12.2023
 

KitaYama

Well-known member
Local time
Today, 19:24
Joined
Jan 6, 2022
Messages
1,541
As for the Doc's suggestion: CDate can only convert certain string formats to a date. The string formats that can be used depend on your own language and country settings. The ISO format should always work:
Access converts the date to local format. In case of Doc's suggestion, when using CDate, no need to use #
The following returns the correct result.

?CDate("31/12/" & DatePart("yyyy", Now()))
2023/12/31
 

gstylianou

Registered User.
Local time
Today, 13:24
Joined
Dec 16, 2013
Messages
357
Access converts the date to local format. In case of Doc's suggestion, when using CDate, no need to use #
The following returns the correct result.

?CDate("31/12/" & DatePart("yyyy", Now()))
2023/12/31
1000 thanks..!!!
 

ebs17

Well-known member
Local time
Today, 12:24
Joined
Feb 7, 2020
Messages
1,946
i did the copied correct..!!
Faith does not have to have anything to do with reality.

DateSerial(Year(Date(), 12, 31) <> DateSerial(Year(Date, 12, 31))

The brackets in Date are not necessary in VBA, but not when used directly in SQL.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:24
Joined
Feb 19, 2002
Messages
43,275
When you don't need the time, DO NOT USE Now()!!!!!!!! Use Date()

It isn't going to matter for this example since you are just pulling the Year out of Now() but you'll look at this example later and in a different situation, you will mess up your calcalculation by using Now() when you only need Date().
 

ebs17

Well-known member
Local time
Today, 12:24
Joined
Feb 7, 2020
Messages
1,946
Access converts the date to local format.
That is not correct. A date is a number (days since 1899-12-30). It is displayed in country format. But: value <> display. The display is like a label on a bottle of wine, legible for the local population.
Conversely, CDate uses regional formats.
With my German settings, the formats used above do not work, but this one does:
Code:
? CDate("31.12.2023")
Now let's see if your own application could or even should be used internationally.
 

KitaYama

Well-known member
Local time
Today, 19:24
Joined
Jan 6, 2022
Messages
1,541

ebs17

Well-known member
Local time
Today, 12:24
Joined
Feb 7, 2020
Messages
1,946
If the term data type is known to the talker: CDate is a type conversion function, not a formatting function. So if you use it, think about it and describe why you use it.
 

KitaYama

Well-known member
Local time
Today, 19:24
Joined
Jan 6, 2022
Messages
1,541
If the term data type is known to the talker: CDate is a type conversion function, not a formatting function. So if you use it, think about it and describe why you use it.
Whatever.
I'm not gonna put my time on an argumentat that goes nowhere.

My apologies if you found my reply to your post agresive or incorrect.
 

Users who are viewing this thread

Top Bottom