Handling DateTime Data Type in C# : Part 1

You can see that Datetime is a data type in C# with library support including methods for accessing time on the computer, such as: take time, current date, yesterday, tomorrow and this year. Surely you have imagined being able to use the publicly accessible time and now you can! This can be applied to many applications related to dates such as application logs, calendars or a computer timer.

  1. Get current date, yesterday, tomorrow:

    In the example below we will get the exact date and time that the program is running through the function DateTime.Now(). The period of time is taken by the system, as well as the time and date format of the system format . DateTime.Today() returns the current date will take longer time for day 0h. To get the day, month and year of the previous day, we use "DateTime.Today().AddDays(-1)"; in other words one day previosuly from the current date. But we see that the results start at the time 12:00:00 AM or 00:00:00. We do not need this value to be able to handle Regular Expression functions. Similarly for the date of tomorrow: DateTime.Today().AddDays() adds or removes days from the current date, in addition to supporting the library AddMonths(), AddYears(), AddHours() ... to support the preceding calculations for better dates.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions; 
    namespace datetime
    {
        class
    Program
        {
            static void Main(string[] args)
            {
                string nowaday =DateTime.Now.ToString();
                Console.WriteLine("Date Time nowaday: {0}", nowaday); 
               
    //yesterday
                string yesterday = DateTime.Today.AddDays(-1).ToString();
                Regex re =new Regex(@"[^ ]*");
                Match m = re.Match(yesterday);
                Console.WriteLine("Yesterday: {0}",m.Groups[0].Value); 
               
    //tomorow
                string tomorow = DateTime.Today.AddDays(1).ToString();
                re = newRegex(@"[^ ]*");
                m = re.Match(tomorow);
                Console.WriteLine("Tomorow: {0}",m.Groups[0].Value); 
               
    //
                Console.Read();
            }
        }
    }

    /*
    Result:
            Date Time nowaday: 4/27/2013 5:25:21 PM
            Yesterday: 4/26/2013
            Tomorow: 4/27/2013
    */
     
  2. Extract date from string: Calculate date

    In the example below we will learn how to extract the date value from a string; the method is Parse(). "SomeDate" will initially have the value "03-03-2008" then the parse method extracts the manufacturing date and the value is assigned to Stardate values. Then take the value of the current date and the total number of days from 03-03-2008 until the current date (the date and time of the current system is 03 06-2011). See the following little bit of code to understand the meaning of each line so I will not explain more extensively.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions; 
    namespace datetime
    {
        class
    Program
        {
            static void Main(string[] args)
            {
               
    // Set random value for datetime string
                string someDate = "03-03-2013"
               
    // Extract DateTime value from string
                DateTime startDate = DateTime.Parse(someDate); 
               
    // Get current day
                DateTime now =DateTime.Now; 
               
    // calculate date...
                TimeSpan elapsed = now.Subtract(startDate);
                int daysAgo = (int)elapsed.TotalDays; 
                Console.WriteLine("From {0} to nowaday have {1} days", someDate, daysAgo.ToString());           
               
    //
                Console.Read();
            }
        }
    }

    /*
    Result:
            From 03-03-2013 to nowaday have 55 days
    */