pemaustria
pemaustria

Reputation: 167

c# - Advancing datetime (duration) from current date

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

Answers (3)

WAQ
WAQ

Reputation: 2626

DateTime.Now.AddDays(5).ToString(@"MMMMMM dd, yyyy HH:mm");

Upvotes: 2

Hari Prasad
Hari Prasad

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

Steve
Steve

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

Related Questions