Showing posts with label DateTime. Show all posts
Showing posts with label DateTime. Show all posts

Saturday, 13 April 2013

You can easily do date time conversion in ASP.Net. You can convert date time from one format to other format.

In real application when data comes from external sources at that time chances increase for date time conversion error because both sender and receiver of data has different local date format setting.

May be you want to display or store date time in different format. If you know that date format that come from outside and you need in different format at that time your task become easy.

Here is example for this.
In this, we have one date in string variable in "MM/dd/yyyy" format. Now we convert this date in "dd/MM/yyyy" format. We are using "TryParseExact" method to parse and create DateTime object from date string after that, we convert in our desire format using "ToString" method. You can convert in any date time format.

Saturday, 16 March 2013

There are situations where we had date in string format in string variable and want to check that this string contains specific date format or not and if this date is in pacific format, then convert it to date time object.

By checking this, we can avoid any date time conversion run time errors. We can do this using "TryParseExact" method of "DateTime" Class.

Here is an Example for this.

Wednesday, 27 February 2013

There are situations where we want date time values in different formats like dd/MM/yy, MM/dd/yy, dd/MMM/yyyy etc.


We are using .ToString() method of date time object to get formatted date time string. In this .ToString() method we need to provide string format in which we want to format date. You can also get time in various format like AM, PM and also get Time Zone like +5:30 etc...

Here is example for this.
In this example we provide various different date time formats. You can also change that accordingly your need. You can also use other symbol than "/".

C#. Net Example :

        DateTime objDateTime = new DateTime(2013, 2, 25, 5, 40, 10);
        Response.Write("<b>dd/MM/yy Date Format :</b>" + objDateTime.ToString("dd/MM/yy"));
        Response.Write("<br/>");
        Response.Write("<b>MM/dd/yy Date Format :</b>" + objDateTime.ToString("MM/dd/yy"));
        Response.Write("<br/>");
        Response.Write("<b>dd/MM/yyyy Date Format :</b>" + objDateTime.ToString("dd/MM/yyyy"));
        Response.Write("<br/>");
        Response.Write("<b>dd-MMM-yyyy Format :</b>" + objDateTime.ToString("dd-MMM-yyyy"));
        Response.Write("<br/>");
        Response.Write("<b>dd/MM/yyyy HH:mm:ss Date And Time Format :</b>" + objDateTime.ToString("dd/MM/yyyy HH:mm:ss"));
        Response.Write("<br/>");
        Response.Write("<b>dd/MM/yyyy HH:mm:ss zzz Date And Time Format with time zone :</b>" + objDateTime.ToString("dd/MM/yyyy HH:mm:ss zzz"));
        Response.Write("<br/>");
        Response.Write("<b>dd/MM/yyyy HH:mm:ss tt Date And Time Format :</b>" + objDateTime.ToString("dd/MM/yyyy HH:mm:ss tt"));
        Response.Write("<br/>");
        Response.Write("<br/>");

VB.Net Example :

            Dim objDateTime As New DateTime(2013, 2, 25, 5, 40, 10)
            Response.Write("<b>dd/MM/yy Date Format : </b>" & objDateTime.ToString("dd/MM/yy"))
            Response.Write("<br/>")
            Response.Write("<b>MM/dd/yy Date Format : </b>" & objDateTime.ToString("MM/dd/yy"))
            Response.Write("<br/>")
            Response.Write("<b>dd/MM/yyyy Date Format : </b>" & objDateTime.ToString("dd/MM/yyyy"))
            Response.Write("<br/>")
            Response.Write("<b>dd-MMM-yyyy Format : </b>" & objDateTime.ToString("dd-MMM-yyyy"))
            Response.Write("<br/>")
            Response.Write("<b>dd/MM/yyyy HH:mm:ss Date And Time Format : </b>" & objDateTime.ToString("dd/MM/yyyy HH:mm:ss"))
            Response.Write("<br/>")
            Response.Write("<b>dd/MM/yyyy HH:mm:ss zzz Date And Time Format with time zone : </b>" & objDateTime.ToString("dd/MM/yyyy HH:mm:ss zzz"))
            Response.Write("<br/>")
            Response.Write("<b>dd/MM/yyyy HH:mm:ss tt Date And Time Format : </b>" & objDateTime.ToString("dd/MM/yyyy HH:mm:ss tt"))
            Response.Write("<br/>")
            Response.Write("<br/>")

Output :
Formatted Date Time In Asp.Net
(To view original image , click on image)


Note : Give us your valuable feedback in comments. Give your suggestions in this article so we can update our articles according to that.


Wednesday, 13 February 2013

We can create date time object by supplying day , month , year , hour , minute and second values. After creating this date time object we can get datetime in our desire format. like short date , long date etc... We are using "ToShortDateString" , "ToLongDateString" , "ToShortTimeString" and "ToLongTimeString" methods.

This is example for this.
In this example we create date time object with day , month , year , hour , minute and second values and print that generated date time on screen.

C#. Net Example :

        DateTime objDateTime = new DateTime(2013, 2, 13, 5, 40, 10);
        Response.Write("<b>Short Date :</b>" + objDateTime.ToShortDateString());
        Response.Write("<br/>");
        Response.Write("<b>Long Date :</b>" + objDateTime.ToLongDateString());
        Response.Write("<br/>");
        Response.Write("<b>Short Time :</b>" + objDateTime.ToShortTimeString());
        Response.Write("<br/>");
        Response.Write("<b>Long Time :</b>" + objDateTime.ToLongTimeString());

VB.Net Examples :

        Dim objDateTime As New DateTime(2013, 2, 13, 5, 40, 10)
        Response.Write("<b>Short Date :</b>" + objDateTime.ToShortDateString())
        Response.Write("<br/>")
        Response.Write("<b>Long Date :</b>" + objDateTime.ToLongDateString())
        Response.Write("<br/>")
        Response.Write("<b>Short Time :</b>" + objDateTime.ToShortTimeString())
        Response.Write("<br/>")
        Response.Write("<b>Long Time :</b>" + objDateTime.ToLongTimeString())

Output :
Create Date Time Object

For more Beginning .Net articles visit this link.  Click Here...

Note : Give us your valuable feedback in comments. Give your suggestions in this article so we can update our articles according to that.