Calculates the number of days until a given date : DateTime « Development « ASP.NET Tutorial






File: DaysUntilDates.asmx


<%@ WebService Language="C#" CodeBehind="~/App_Code/DaysUntilDates.cs" Class="DaysUntilDates" %>

File: \App_Code\DaysUntilDates.cs

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;


[WebService(Namespace = "http://tempuri.com/", Description="Calculates the number of days until a given date.")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class DaysUntilDates : System.Web.Services.WebService {

    [WebMethod]
    public int DaysUntil(DateTime date)
    {
        return DaysUntilDate(date.Month, date.Day);
    }

    [WebMethod]
    public int DaysUntilHalloween()
    {
        return DaysUntilDate(10, 31);
    }

    [WebMethod]
    public int DaysUntilChristmas()
    {
        return DaysUntilDate(12, 25);
    }

    private int DaysUntilDate(int month, int day)
    {
        DateTime targetDate;
        targetDate = DateTime.Parse(month.ToString() + "/"
                   + day.ToString() + "/"
                   + DateTime.Today.Year);
        if (DateTime.Today > targetDate)
        {
            targetDate = targetDate.AddYears(1);
        }
        TimeSpan timeUntil = targetDate - DateTime.Today;
        return timeUntil.Days;
    }    
}








9.11.DateTime
9.11.1.Today.Ticks
9.11.2.Assign DateTime value to asp:Label (C#)
9.11.3.Set the asp:Label in code behind based on current time (VB.net)
9.11.4.DateTime.Now.ToShortDateString()
9.11.5.Calculates the number of days until a given date
9.11.6.DateTime and TimeSpan
9.11.7.Format DateTime
9.11.8.Format DateTime variable (VB)
9.11.9.Format DateTime variable (C#)