Thursday, 19 March 2015

How to Change DateTime Format Using C# In Asp.Net

3/19/2015 - By Pranav Singh 0

In this article I will show how to change datetime format using c# in asp.net. in this code I have demonstrated the date format conversion using c# in asp.net.


So for this article we will create a new asp.net application and add the below code.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to Change DateTime Format Using C# In Asp.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Date 1 :
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
        Date 2 :
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
        Date 3 :
        <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label><br />
        Date 4 :
        <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label><br />
        Date 5 :
        <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label><br />
        Date 6 :
        <asp:Label ID="Label6" runat="server" Text="Label"></asp:Label><br />
        Date 7 :
        <asp:Label ID="Label7" runat="server" Text="Label"></asp:Label><br />
        Date 8 :
        <asp:Label ID="Label8" runat="server" Text="Label"></asp:Label><br />
        Date 9 :
        <asp:Label ID="Label9" runat="server" Text="Label"></asp:Label><br />
        Date 10 :
        <asp:Label ID="Label10" runat="server" Text="Label"></asp:Label><br />
        Date 11 :
        <asp:Label ID="Label11" runat="server" Text="Label"></asp:Label><br />
        Date 12 :
        <asp:Label ID="Label12" runat="server" Text="Label"></asp:Label><br />
        Date 13 :
        <asp:Label ID="Label13" runat="server" Text="Label"></asp:Label><br />
        Date 14 :
        <asp:Label ID="Label14" runat="server" Text="Label"></asp:Label><br />
    </div>
    </form>
</body>
</html>

Now we will add the code into our code behind to display the date In different format.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = DateTime.Now.ToString();
            /*Sate time fomrat*/
            DateTime dt = new DateTime();
            dt = DateTime.Now;
            Label2.Text = String.Format("{0:t}", dt);//h:mm tt
            Label3.Text = String.Format("{0:d}", dt);//M/d/yyyy
            Label4.Text = String.Format("{0:T}", dt);//h:mm:ss tt
            Label5.Text = String.Format("{0:D}", dt);//dddd, MMMM dd, yyyy
            Label6.Text = String.Format("{0:f}", dt);//dddd, MMMM dd, yyyy h:mm tt
            Label7.Text = String.Format("{0:F}", dt);//dddd, MMMM dd, yyyy h:mm:ss tt
            Label8.Text = String.Format("{0:g}", dt);//M/d/yyyy h:mm tt
            Label9.Text = String.Format("{0:G}", dt);//M/d/yyyy h:mm:ss tt
            Label10.Text = String.Format("{0:m}", dt);//MMMM dd
            Label11.Text = String.Format("{0:y}", dt);//MMMM, yyyy
            Label12.Text = String.Format("{0:r}", dt);//ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
            Label13.Text = String.Format("{0:s}", dt);//yyyy'-'MM'-'dd'T'HH':'mm':'ss
            Label14.Text = String.Format("{0:u}", dt);//yyyy'-'MM'-'dd HH':'mm':'ss'Z'
        }
    }
}

Now we have done run the application to check the output.


DOWNLOAD

Tags: , ,
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top