Reputation: 167
I want to display date advancing the current date (like validity date) on a button click. My code are as follows:
protected void Button1_Click(object sender, EventArgs e)
{
lblDateToday = DateTime.Now.ToString("MMMMMM dd, yyyy HH:mm");
lblValiDate = <output date: 5 days from the current>
}
Any ideas? Thanks in advance!
Upvotes: 1
Views: 123
Reputation: 16956
you can use AddDays
to add days to your current date.
DateTime dt = DateTime.Now;
lblDateToday = dt.ToString("MMMMMM dd, yyyy HH:mm");
lblValiDate = dt.AddDays(5).ToString("MMMMMM dd, yyyy HH:mm");
Fiddler Demo
Upvotes: 0
Reputation: 9571
Get the current DateTime as a DateTime type, then you can use the AddDays extension method to increment the Date.
protected void Button1_Click(object sender, EventArgs e)
{
DateTime dateToday = DateTime.Now;
DateTime dateInFiveDays = dateToday.AddDays(5);
lblDateToday = dateToday.ToString("MMMMMM dd, yyyy HH:mm");
lblValiDate = dateInFiveDays.ToString("MMMMMM dd, yyyy HH:mm");
}
https://msdn.microsoft.com/en-us/library/system.datetime.adddays(v=vs.110).aspx
Upvotes: 2