String format date
string-format-date.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<%@ Import Namespace="System.Globalization" %>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
//this line create a culture for english in us
CultureInfo usCulture = new CultureInfo("en-US");
CultureInfo frCulture = new CultureInfo("fr-FR");
CultureInfo gbCulture = new CultureInfo("en-GB");
CultureInfo dkCulture = new CultureInfo("da-DK");
//this line create a datetime variable.
DateTime now = DateTime.Now;
Label1.Text = "now: " + now.ToString();
//format string using ToString method.
Label1.Text += "<br /><br />short date: " + now.ToString("d");
Label1.Text += "<br />long date: " + now.ToString("D");
Label1.Text += "<br />full date and short time: " + now.ToString("f");
Label1.Text += "<br />full date and long time: " + now.ToString("F");
Label1.Text += "<br />general date time (short time): " + now.ToString("g");
Label1.Text += "<br />general date time (long time): " + now.ToString("G");
Label1.Text += "<br />month pattern: " + now.ToString("M");
Label1.Text += "<br />year pattern: " + now.ToString("Y");
//string format date using string.format method.
Label1.Text += "<br /><br />date time format in [en-US] culture: " + string.Format(usCulture, "{0:d}", now);
Label1.Text += "<br />date time format in [fr-FR] culture: " + string.Format(frCulture, "{0:d}", now);
Label1.Text += "<br />date time format in [en-GB] culture: " + string.Format(gbCulture, "{0:d}", now);
Label1.Text += "<br />date time format in [da-DK] culture: " + string.Format(dkCulture, "{0:d}", now);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - string format date</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - string format date
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="string format date"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>

- How to format a string as a phone number
- How to format a string as a number with leading zero
- How to format a string as a number with 2 decimal places
- How to format a string as a number with sign
- How to format a string as a fixed length number
- How to format a string as currency without decimal
- How to format a string as currency with dollar sign
- How to format a string as negative currency
- How to format a date string as yyyy-MM-dd format
- How to format a string as a date with milliseconds