How to add and subtract hours and days to or from current Date-Time.


This ASP.NET C# code snippest will show you how you can show an formatted current DateTime in your web page, how to subtract Days/Hours from current DateTime and also show you how to add Days/Hours in current DateTime.

With this code we can show the current system date-time in which format we want. We can aad days, hours, minutes and seconds in that date-time. Also can subtract days, hours, minutes and seconds from that date-time. This is very useful in registraion process. It is also useful when we want to calculate an date-time after or before current date-time. Now I'm going to implementation of this code.
The code is given below-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString("MMMM dd, yyyy hh:mm:ss tt");
Label2.Text = DateTime.Now.Add(TimeSpan.FromHours(24)).ToString("MMMM dd, yyyy hh:mm:ss tt");
Label3.Text = DateTime.Now.Subtract(TimeSpan.FromDays(365)).ToString("MMMM dd, yyyy hh:mm:ss tt");
Label4.Text = DateTime.Now.Add(TimeSpan.FromMinutes(60)).ToString("MMMM dd, yyyy hh:mm:ss tt");
Label5.Text = DateTime.Now.Subtract(TimeSpan.FromSeconds(60)).ToString("MMMM dd, yyyy hh:mm:ss tt");
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: