We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Friday, April 27, 2018


Date time format in c#


How to remove time in datetime in C#

static void Main(string[] args)
        {
            DateTime myDate = System.DateTime.Now;

            Console.WriteLine(myDate.ToShortDateString());
            Console.ReadLine();

        }

Output:


How to display time in Date Time in C#

static void Main(string[] args)
        {
            DateTime myDate = System.DateTime.Now;

            Console.WriteLine(myDate.ToShortTimeString());
            Console.ReadLine();

        }

Output:


How to display name of month in Date Time in C#

static void Main(string[] args)
        {
            DateTime myDate = System.DateTime.Now;

            Console.WriteLine(myDate.ToLongDateString());
            Console.ReadLine();

        }
Output:


How to display hour, minute and  second in Date Time in C#

static void Main(string[] args)
        {
            DateTime myDate = System.DateTime.Now;

            Console.WriteLine(myDate.ToLongTimeString());
            Console.ReadLine();

        }
Output:


How to add date in current date in C#
  
static void Main(string[] args)
        {
            DateTime myDate = System.DateTime.Now;
            Console.WriteLine("Current date   {0}:", myDate.ToLongDateString());
            Console.WriteLine("After add date   {0}:", myDate.AddDays(5).ToLongDateString());
            Console.ReadLine();

        }
Output:


How to add time in current time in C#
  
static void Main(string[] args)
        {
            DateTime myDate = System.DateTime.Now;
            Console.WriteLine("Current hour   {0}:", myDate.ToShortTimeString());
            Console.WriteLine("After add hour   {0}:", myDate.AddHours(5).ToShortTimeString());
            Console.ReadLine();

        }
Output:


How to display only moth in DateTime in C#
  
static void Main(string[] args)
        {
            DateTime myDate = System.DateTime.Now;
            Console.WriteLine("Current month:  {0}",myDate.Month);
            Console.ReadLine();

        }
Output:


How to convert numbers to date time in C#

static void Main(string[] args)
        {
            DateTime myDate = new DateTime(1990, 04, 14);
            Console.WriteLine(myDate.ToShortDateString());
            Console.ReadLine();

        }
Output:


Calculate age in date from birth-date to current date in C#
  
static void Main(string[] args)
        {
            DateTime myBirthday= DateTime.Parse("14/04/1990");
            TimeSpan myAge = DateTime.Now.Subtract(myBirthday);
            Console.WriteLine(myAge.TotalDays);
            Console.ReadLine();

        }


Output:

No comments: