welcome to XRM blog

Keep in touch with latest CRM/ERP articles

To remain competitive your organisation must be efficient across the business process spectrum. To do so you need to take sound decisions based on a balance between the cost and risk. To do so you will be heavily dependent on your content management in itself needs...

image
Blog

ASP.NET Date Manipulation

By xrmlabs webmaster on 3/20/2013

ASP.NET Date Manipulation

 In this article I want to describe the date manipulation in c# using SQL SERVER this article covers storing date and time in SQL data base as well as manipulation with c# programming using various date formats which are used on the ASP.NET UI  forms.

 How the SQL server stores date   in the database :  IN the SQL server datetime field stores the date in the format of dd/mm/yy hh:mm:ss . if the time is not mentioned the time is stored default as 12:00:00 AM  . It can be seen by these two screen shots

 Screen shot1 as web page where there are two date fields one is Date of Birth and  second one is session date , in the date of birth field three drop down list are dynamically used however the end user can select the date in dd/mm/yy format , in the same way the user can select the session date by using calendar in dd/mm/yy format it is a illusion of the end user that he is able to select the date in dd/mm/yyyy format but actually the dates are stored in the SQL server only in mm/dd/yyyy format this can be easily verified by the label session date where the date  has been selected by the calendar is displayed on the label in mm/dd/yy  format .How these dates are stored in the SQL server  it is displayed in the next screen shot2 and screenshot3 .

Screen shot1 selecting the dates by drop down list or using calendar:



So it is clear that  in the database the date is stored in  mm/dd/yyyy then  how can we display the only date , only time , both date and time in the desired format  on the web page .

 

Here is the various format to display the date and time .

Format  

Specifier   

Pattern

Output  displayed ( Example)

t

h:mm tt

4:05 PM

d

M/d/yyyy

3/9/2013

T

h:mm:ss tt

4:05:07 PM

D

dddd, MMMM dd, yyyy

Sunday, March 09, 2013

f

dddd, MMMM dd, yyyy h:mm tt

Sunday, March 09, 2013 4:05 PM

F

dddd, MMMM dd, yyyy h:mm:ss tt

Sunday, March 09, 2013 4:05:07 PM

g

M/d/yyyy h:mm tt

3/9/2013 4:05 PM

G

M/d/yyyy h:mm:ss tt

3/9/2013 4:05:07 PM

m,M

MMMM dd

March 09

y,Y

MMMM, yyyy

March, 2013

r, R 

(RFC1123Pattern)

ddd, dd MMM yyyy HH':'mm':'ss 'GMT'



Example:


Suppose we want to display  the   date time 2013-03-09 16:05:07.123

DateTime dt = new DateTime(2013, 3, 9, 16, 5, 7, 123);

 

An example to use of these format specifier  in string.Format  method .

String.Format("{0:t}", dt);  // "4:05 PM"   

String.Format("{0:d}", dt);  // "3/9/2013"  

Custom DateTime Formatting:  Suppose if we don’t get our desired format in the above table we can design our own custom format .

Where  format specifiers y (year), M (month), d (day), h (hour 12), H (hour 24), m (minute), s (second), f (second fraction), F (second fraction, trailing zeroes are trimmed), t (P.M or A.M)

Various ways to display date time custom format :

Name

Format

Display format

Year

y yy yyy yyyy

9 09 009 0009

Month

M MM MMM MMMM

3 03 Mar March

Day

d dd ddd dddd

9 09 Sun Sunday

Hour 12/24

h hh H HH

5 05 17 17

Minute

m mm

5 05

Seconds

s ss

7 07

Second Fraction

f ff fff ffff

1 12 123 1230

Second Fraction without zeros

F FF FFF FFFF

1 12 123 123

A.M. or P.M.

t tt

P PM  or A AM

Time Zone

z zz zzz

"-6 -06 -06:00



(You can use , - or / according to your requirement.)

Example :

 DateTime dt = DateTime.Now; //out put will be 3/19/2013 11:17:29

 DateTime dt = DateTime.Today.Date;//label 3/19/2013 12:00:00

 Label1.Text = DateTime.Now.ToString("d/M/yyyy");//out put  9/3/2013 

 Label1.Text = DateTime.Now.ToString("dd/MM/yyyy");//out put  09/03/2013

 Label1.Text = DateTime.Now.ToString("MMMM, ddd dd, HH:mm yyyy”); //out put 09/03/2013

                                               

Date manipulation in C #

 

As  you know that the static members of the class are accessed by the class there is no need to create the object of the class so for using the current date time  we can use the static property of the DateTime class .

 

Example :

DateTime dt = DateTime.Now; //It displays the today's date with current time 

e.g.6/19/2012 12:00:00

DateTime dt = DateTime.Today.Date;  //It displays the today's date with start time of the date  e.g.6/19/2012 12:00:00 (Takes the system time )

 

Date Manipulation : While manipulation with dates you should care of the date format because of date can be subtracted , compared  or added only when the date is taken in the mm/dd/yyyy format  while you can display the date or time in any your desired format.

Calculating the difference between two dates adding days in the start date :

In this example the end date is calculated after adding  365 days and the end date is displayed  in local time  we know generally the system time is set to the time zone of  the local time  .

Example 1:

protected void Button1_Click1(object sender, EventArgs e)
 
    {
 
        
 
        DateTime StartDate = DateTime.Today.Date;
 
        DateTime EndDate = StartDate.AddDays(365);
 
        TimeSpan ts = EndDate.Subtract(StartDate);
 
        Label1.Text = "total days" + ts.Days.ToString();
 
        Label2.Text = "hours" + ts.Hours.ToString();
 
        Label3.Text = "Minutes" + ts.Minutes.ToString();
 
        Label4.Text = "today date" + StartDate.ToString();     
 
        Label5.Text = "Miliseconds" + ts.Milliseconds.ToString();
 
        Label6.Text = "difference" + ts.ToString();
 
        EndDate = EndDate.ToUniversalTime();
 
        DateTime EndDateLocalTime = EndDate.ToLocalTime();
 
        Label8.Text = "End date loacl time " + EndDateLocalTime.ToString();
 
    }

Output of the Program:

total days365
hours0
Minutes0
today date7/1/2012 12:00:00 AM
Miliseconds0
difference365.00:00:00
End date loacl time 7/1/2013 12:00:00 AM

In the above  example the difference is calculated  after adding for 365 days  then end date is displayed at the local time  .

Example 2: In this example the difference is calculated after adding  365 days to the today’s date  the end date is displayed  in the    UTC date time  as for time zone the difference is (UTC+ 5.30) that is why there is a difference of 5.30 hours.

                  

protected void Button1_Click(object sender, EventArgs e)
  
    {
               
  DateTime StartDate = DateTime.Today.Date;//Takes the system date time 12:00 A.M.
  
        DateTime EndDate = StartDate.AddDays(365);
  
        TimeSpan ts = EndDate.ToUniversalTime().Subtract(StartDate);
  
        TimeSpan ts = EndDate.Subtract(StartDate);  
  
        txtDifference.Text = ts.ToString();
  
       Label1.Text = "total days" + ts.Days.ToString();
  
        Label2.Text = "hours" + ts.Hours.ToString();
  
        Label3.Text = "Minutes" + ts.Minutes.ToString();
  
        Label4.Text = "today date" + StartDate.ToString();
  
        Label5.Text = "END DATE UTC" + EndDate.ToUniversalTime().ToString();
  
        Label6.Text = "Miliseconds" + ts.Milliseconds.ToString();
  
        Label7.Text = "difference" + ts.ToString();
  
    }

Output :

total days364
hours18
Minutes30
today date 6/21/2012 12:00:00 AM
END DATE UTC6/20/2013 6:30:00 PM
Miliseconds0
difference364.18:30:00

 










.Net
ASP.NET
C#
Date manipulation
Blog Calendar
Blog Calendar List
2024 Apr  2  4
2024 Mar  20  4
2024 Feb  22  3
2024 Jan  7  7
2023 Dec  10  6
2023 Nov  32  5
2023 Oct  88  12
2023 Sep  200  9
2023 Aug  57  7
2023 Jul  31  5
2023 Jun  20  4
2023 May  43  5
2023 Apr  32  5
2023 Mar  90  6
2023 Feb  106  5
2023 Jan  37  4
2022 Dec  94  7
2022 Nov  250  2
2022 Sep  13  1
2022 Aug  27  2
2022 Jun  7  2
2022 May  3  2
2022 Apr  6  2
2022 Mar  1  1
2022 Feb  2  1
2022 Jan  1  1
2021 Dec  3  1
2021 Nov  2  1
2021 Oct  1  1
2021 Sep  11  1
2021 Aug  37  5
2021 Jul  36  4
2021 Jun  1210  5
2021 May  31  3
2021 Apr  1997  3
2021 Mar  188  5
2021 Feb  2094  7
2021 Jan  2979  9
2020 Dec  434  7
2020 Sep  73  3
2020 Aug  672  3
2020 Jul  126  1
2020 Jun  75  3
2020 Apr  68  3
2020 Mar  12  2
2020 Feb  27  5
2020 Jan  34  7
2019 Dec  17  4
2019 Nov  29  1
2019 Jan  23  2
2018 Dec  63  4
2018 Nov  68  3
2018 Oct  18  3
2018 Sep  1132  11
2018 Aug  7  2
2018 Jun  13  1
2018 Jan  68  2
2017 Sep  585  5
2017 Aug  17  1
2017 Jul  17  2
2017 Jun  62  2
2017 May  21  1
2017 Apr  35  2
2017 Mar  135  4
2017 Feb  773  4
2016 Dec  203  3
2016 Nov  823  8
2016 Oct  304  10
2016 Sep  697  6
2016 Aug  39  1
2016 Jun  1871  6
2016 May  110  3
2016 Jan  71  2
2015 Dec  472  6
2015 Nov  4  1
2015 Oct  13  1
2015 Sep  1464  6
2015 Aug  14  1
2015 Jul  128  2
2015 Jun  10  1
2015 May  20  1
2015 Apr  30  3
2015 Mar  80  3
2015 Jan  5335  4
2014 Dec  17  1
2014 Nov  2257  4
2014 Oct  68  1
2014 Sep  107  2
2014 Aug  5272  1
2014 Jul  48  2
2014 Apr  2578  12
2014 Mar  300  17
2014 Feb  220  6
2014 Jan  1510  16
2013 Dec  21  2
2013 Nov  689  2
2013 Oct  256  3
2013 Sep  11  1
2013 Aug  40  3
2013 Jul  214  1
2013 Apr  57  6
2013 Mar  2282  10
2013 Feb  127  3
2013 Jan  341  2
2012 Nov  57  2
2012 Oct  499  10
Tag Cloud
Interested in our services? Still not sure about project details? get a quote