Handling DateTime Data Type in C# : Part 1

Introduction

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 computer timers.

Get the 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 a longer time for day0h. To get the day, month, and year of the previous day, we use "DateTime.Today().AddDays(-1)"; in other words one day previously 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(), and AddHours() ... to support the preceding calculations for better dates.

using System;

namespace datetime
{
    class Program
    {
        static void Main(string[] args)
        {
            string nowaday = DateTime.Now.ToString();
            Console.WriteLine($"Date Time nowaday: {nowaday}");

            // Yesterday
            string yesterday = DateTime.Today.AddDays(-1).ToString();
            var re = new System.Text.RegularExpressions.Regex(@"[^ ]*");
            var m = re.Match(yesterday);
            Console.WriteLine($"Yesterday: {m.Groups[0].Value}");

            // Tomorrow
            string tomorrow = DateTime.Today.AddDays(1).ToString();
            re = new System.Text.RegularExpressions.Regex(@"[^ ]*");
            m = re.Match(tomorrow);
            Console.WriteLine($"Tomorrow: {m.Groups[0].Value}");
        }
    }
}

Output

date

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;

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
            DateTime now = DateTime.Now;

            // Calculate days difference
            TimeSpan elapsed = now - startDate;
            int daysAgo = (int)elapsed.TotalDays;

            Console.WriteLine("From {0} to nowaday have {1} days", someDate, daysAgo);

            // Console.Read();
        }
    }
}

Output

forEachDate


Similar Articles