Friday, 5 October 2012

How to Subtract the two date in C#- C-Sharp

here we subtract the two date start date and end date and store in a variavle in Number of Days Format
//First we extract the ate from data table and store in a variable

  string endt=dt.Rows[(dt.Rows.Count - 1)]["DATE_FIELD"].ToString() ;  //It gives the END Date in '14-May-2012' Format
  
  string stdt =dt.Rows[0]["DATE_FIELD"].ToString();   // //It gives the Start Date in '14-May-2012' Format 
                    
  int daydiff = 0;   // Initialize the variable

  // In the below case we subtract the date and store in a variavle in day format, like how many number of days
  daydiff = Convert.ToInt32( Convert.ToDateTime(endt).ToString("yyyyMMdd")) -Convert.ToInt32( Convert.ToDateTime(stdt).ToString("yyyyMMdd"));


Monday, 1 October 2012

SET DATE TIME FORMAT IN C#


using System;

class Program
{
    static void Main()
    {
 DateTime time = DateTime.Now;              // Use current time
 string format = "MMM ddd d HH:mm yyyy";    // Use this format
 Console.WriteLine(time.ToString(format));  // Write to console
    }
}

MMM display three-letter month ddd display three-letter day of the WEEK d display day of the MONTH HH display two-digit hours on 24-hour scale mm display two-digit minutes yyyy display four-digit year



 System.DateTime.Now.ToString("dd-MMM-yyyy HH:mm:ss")
Refrence: http://www.dotnetperls.com/datetime-format