Click here to Skip to main content
15,881,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to have an if statement that goes like this:

int yrcount = 18;
SR_txtyr.Text = yrcount.ToString();

If year.now adds 1 then
yrcount adds 1


Basically, I want it to do is add 1 into my variable if the date proceeds to next year.

The textbox is disabled so it can't be altered. It's more part of a document number that changes every year.

What I have tried:

I don't know how date time works, sadly.
Posted
Updated 21-Jun-18 21:11pm
Comments
GKP1992 22-Jun-18 2:36am    
Using the DateTime class, you can use the year property of the date and assign it to your variable.

1 solution

If i understand you properly...

To get current year, use this:
C#
int currentyear = DateTime.Today.Year;


or (even better):
C#
DateTime currentdate = DateTime.Today;
int currentyear = currentdate.Year;


For further details, please see: DateTime Structure (System)[^]

[EDIT]

C#
DateTime currentdate = DateTime.Today;
DateTime nextyeardate = DateTime.Today.AddMonths(8);
int currentyear = currentdate.Year;
int nextyear = nextyeardate.Year;

if (nextyear-currentyear==1)
{
	Console.WriteLine("Next year!");
}
else
{
	Console.WriteLine(":(");
}


if-else (C# Reference) | Microsoft Docs[^]
 
Share this answer
 
v4
Comments
[no name] 22-Jun-18 3:16am    
Yes, I've covered that part already. Now, I was wondering how could I know if the year moved from 2018 to 2019. I'm at the IF part actually.
Maciej Los 22-Jun-18 3:22am    
OK. Check my answer again ;)
[no name] 22-Jun-18 4:04am    
1 question: What's the purpose of the value 8 in AddMonths?

Also, I've tried moving the computer date. When I got to 2020, It stopped adding.
Maciej Los 22-Jun-18 7:09am    
Just an example, how to add 8 months to current date to get next-year date ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900