Wednesday, 27 February 2013

Beginning .Net : Get formatted date time string from date time object in asp.net

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.


No comments:

Post a Comment