Search This Blog

Tuesday, February 11, 2014

Date format in Asp.net Examples

How to Insert Record Instead of Default Date format to dd/mm/yyyy or dd-mm-yyyy???


Default Date Format in C sharp is 

Example :- 

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime d = new DateTime();
            Console.WriteLine(d);
            Console.ReadLine();
        }
    }
}

Then Default Output is 1/1/0001 12:00:00 AM

So, We must to change or Give the datetime of now to d.

DateTime d = System.DateTime.Now;

The it will Give Output As Current Date and Time :- mm/dd/yyyy hh:mm:ss am/pm

Indian Standard time output is :   2/11/2014 5:30:54 PM


If i have to take universal Time then use below Function

DateTime d = System.DateTime.Now.ToUniversalTime();


output is 2/11/2014 12:00:54 PM

Convert into Short date format mm/dd/yyyy.

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            String d = System.DateTime.Now.ToShortDateString();
            Console.WriteLine(d);
            Console.ReadLine();
        }
    }
}
output :- mm/dd/yyyy


Convert into Short time format hh:mm 


            String d = System.DateTime.Now.ToShortTimeString();
output :- 5:30 PM

Convert into ole automation date.
This format is Normally used with COM and VB scripting purpose of automatic scripting after some time the code will be executed. Return type of this function is float.


namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            double d = System.DateTime.Now.ToOADate();
            Console.WriteLine(d);
            Console.ReadLine();
        }
    }
}

output :- 41681.7406242824


If we have to print only current year then

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int d = System.DateTime.Now.Year;
            Console.WriteLine(d);
            Console.ReadLine();
        }
    }
}
 output :- 2014

To print Month then use.

int d = System.DateTime.Now.Month;

Output: 2


To print Date then use

int d = System.DateTime.Now.Day;

Output : 11


Print Hour of the date
int d = System.DateTime.Now.Hour;
output : 17 instead of 5 PM


Print Hour of the date
int d = System.DateTime.Now.Minute;
output : 30

To print Millisecond

int d = System.DateTime.Now.Millisecond;
output : 534



To print only time Use Timespan

syntax of output : hours:minutes:seconds

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            TimeSpan d = System.DateTime.Now.TimeOfDay;
            Console.WriteLine(d);
            Console.ReadLine();
        }
    }
}

Output :- 17:30:01.2312332


Which kind of time is taken into the Datetime we can check weather it is Utc or local?

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTimeKind d = System.DateTime.Now.ToUniversalTime().Kind;
            Console.WriteLine(d);
            Console.ReadLine();
        }
    }
}

output : Utc

 DateTimeKind d = System.DateTime.Now.Kind;

output : Local

IF we have to add years in date then we can add.


namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime d = System.DateTime.Now.AddYears(2);
            Console.WriteLine(d);
            Console.ReadLine();
        }
    }
}
output will print date and time of 2016.
output is 2/11/2016 12:00:54 PM


Increase Month using AddMonth(int)

DateTime d = System.DateTime.Now.AddMonths(2);

Then Month is April.

output is 4/11/2016 12:00:54 PM


Increase Date using AddDays(int)

DateTime d = System.DateTime.Now.AddDays(2);

output is 2/13/2016 12:00:54 PM




1 comment:

  1. If You are interested for Learning Asp.net Project Training then visit website:
    www.vataliyatuitionclasses.com

    ReplyDelete