Resolved Textbox to display date one year from today

PDS8475

Active member
Joined
Jun 25, 2019
Messages
41
Programming Experience
Beginner
I have a form that on load displays the date now in a text box. I also want it to display the date one year from now in another textbox but I do not know how to achieve this. Looking online I can only find examples of date now as text.
 
You have marked your question Resolved but you have not provided a solution, so we don't know whether you have the best one and this thread can't help anyone else in future. With that in mind, you should call AddYears on your existing DateTime and it will produce a new value with the specified number of years added.
 
C#:
            DateTime dtn = DateTime.Now.Date;
            textBox1.Text = $"Date is : {dtn.ToString("dd/MM/yyyy")} : In one year it will be : {dtn.Date.AddYears(1).ToString("dd/MM/yyyy")}";
Outputs Date is : 13/10/2019 : In one year it will be : 13/10/2020
 
C#:
            DateTime dtn = DateTime.Now.Date;
            textBox1.Text = $"Date is : {dtn.ToString("dd/MM/yyyy")} : In one year it will be : {dtn.Date.AddYears(1).ToString("dd/MM/yyyy")}";
Outputs Date is : 13/10/2019 : In one year it will be : 13/10/2020
On an unrelated note, I would point out that Date.Today is equivalent to Date.Now and, in fact, is specifically implemented that way. It's just a bit more self-documenting. That said, if you're going to format the date anyway and not actually use it in a way that the time matters, you may as well just use Date.Now. Small things but things none the less.
 
DateTime.Now is for date and time whereas DateTime.Today can be used for dates. Also note op wants date and not time too. So yea, I agree, If you argue semantically, you can make a date with both...
 
DateTime.Now is for date and time whereas DateTime.Today can be used for dates. Also note op wants date and not time too. So yea, I agree, If you argue semantically, you can make a date with both...
The OP wants to display a date with no time, which is done by formatting it as a String, which means that it doesn't matter whether the original DateTime had the time zeroed or not. DateTime.Today.ToString("dd/MM/yyyy") and DateTime.Now.ToString("dd/MM/yyyy") will produce the same result, so zeroing the time serves no purpose. Imagine what you could be doing in the 1.5 nanoseconds you would save. ;) Of course, it depends on what else you might want to use the value for in code. If it's for more than just display, having the time zeroed may well be preferable.
 
The difference is extremely minimal in this instance, and doesn't particularly matter. From OP's past posts, I don't think they're processing millions of dates in an iteration cycle. So again, it's still semantics IMO. But point noted. ;)
 
It's actually rather pointless. Unless you know it's only trying to encourage a healthy discussion and neither side will take the argument too seriously, then it sure can be.
 
Back
Top Bottom