Minifycode 2021-10-03 Viewed 2.6K times C#

使用C#的日期時間格式

 

c#中的DateTime格式:在本文中,如何在C#中使用DateTime格式。我們將格式化字符串與DateTime和ToString一起使用。在這裡,我們結合了許多格式代碼。另外:使用DateTime.ParseExact和DateTime.ParseExact時,我們需要指定格式字符串。
修改格式。在這裡,我們修改格式字符串以獲取不同的輸出。我們更改某些字段,結果字符串值較短。

使用其他格式的C#程序

使用系統;

班級計劃
{
    靜態void Main()
    {
        DateTime時間= DateTime.Now;
        字符串dtformat =“ M d h:mm yy”;
        Console.WriteLine(time.ToString(dtformat));
    }
}

輸出量

09 13 11:48 20

格式化字符串模式

M顯示一位數字的月份號
d顯示月份的一位數字
h在12小時上顯示一位數字小時
毫米顯示兩位數分鐘
yy顯示兩位數年份
使用DateTime格式的C#程序

使用系統;

班級計劃
{
    靜態void Main()
    {
        //使用當前時間和格式字符串。
        DateTime時間= DateTime.Now;
        字符串dtformat =“ MMM ddd d HH:mm yyyy”;
        Console.WriteLine(time.ToString(dtformat));
    }
}

輸出量

2020年2月2日星期二13:15

格式化字符串模式

MMM顯示三個字母的月份
ddd顯示一周中的三個字母的一天
d顯示月份
HH在24小時顯示兩位數的小時數
毫米顯示兩位數分鐘
yyyy顯示四位數的年份
這些是標準格式。它們在許多程序中很有用。單一char DateTime格式。我們將字符與ToString或DateTime.ParseExact一起使用以指定預設格式。
DateTime.Parse

使用系統;

班級計劃
{
    靜態void Main()
    {
        Console.WriteLine(dt.ToString(“ d”));
        Console.WriteLine(dt.ToString(“ D”));
        Console.WriteLine(dt.ToString(“ f”));
        Console.WriteLine(dt.ToString(“ F”));
        Console.WriteLine(dt.ToString(“ g”));
        Console.WriteLine(dt.ToString(“ G”));
        Console.WriteLine(dt.ToString(“ m”));
        Console.WriteLine(dt.ToString(“ M”));
        Console.WriteLine(dt.ToString(“ o”));
        Console.WriteLine(dt.ToString(“ O”));
        Console.WriteLine(dt.ToString(“ s”));
        Console.WriteLine(dt.ToString(“ t”));
        Console.WriteLine(dt.ToString(“ T”));
        Console.WriteLine(dt.ToString(“ u”));
        Console.WriteLine(dt.ToString(“ U”));
        Console.WriteLine(dt.ToString(“ y”));
        Console.WriteLine(dt.ToString(“ Y”));
DateTime dt = DateTime.Now;
    }
}

輸出量


D 2020年9月11日星期五
f 2020年9月11日(星期五)12:11 PM
F 2020年9月11日星期五12:12:22 PM
g 9/11/2020 12:12 PM
G 9/11/2020 12:12:22 PM
m 9月11日
M 9月11日
o 2020-09-11T12:12:22.1020000-08:00
O 2020-09-11T12:12:22.1020000-08:00
s 2020-09-11T12:12:22
t 12:12下午
下午12:12:22
u 2009-02-27 12:12:22Z
U 2020年9月11日星期五晚上8:12:22
y 2020年9月
Y 2020年9月
d 9/13/2020
日期字符串:在這裡,我們看到了DateTime上的ToLongTimeString,ToLongDateString,ToShortTimeString和ToShortDateString方法。

 

字符串格式
使用ToString方法的C#程序

使用系統;

班級計劃
{
    靜態void Main()
    {
        DateTime現在= DateTime.Now;
        //等同於D。
        Console.WriteLine(now.ToLongDateString());
        //等於T。
        Console.WriteLine(now.ToLongTimeString());
        //等於d。
        Console.WriteLine(now.ToShortDateString());
        //等於t。
        Console.WriteLine(now.ToShortTimeString());
        Console.WriteLine(now.ToString());
    }
}
輸出量

ToLongDateString 2020年9月11日,星期五
ToLongTimeString 12:20:59 PM
ToShortDateString 9/11/2020
ToShortTimeString 12:16 PM
ToString 9/11/2020 12:20:59 PM
分鐘格式:如果數字只有一位數字,則兩個小寫ms的前導零。我們在幾分鐘內使用小寫代碼“ mm”。在幾分鐘內,我們必須使用2個字符。
注意:僅一個“ m”不返回分鐘,而是返回月和日。

顯示分鐘部分的C#程序

使用系統;

班級計劃
{
    靜態void Main()
    {
        //用5分鐘創建DateTime。
        DateTime nineMinutes =新的DateTime(2020,4,5,3,5,0);
        Console.WriteLine(“ mm:{0}”,nineMinutes.ToString(“ mm”));
    }
}
輸出量

毫米:05


小寫s:小寫s顯示秒。使用ss,我們總是需要兩位數字,例如00-59。

使用S格式的C#程序

使用系統;
使用靜態System.Console;

班級計劃
{
    靜態void Main()
    {
        DateTime dt = DateTime.Now;
        //在s之後使用空格,以避免使用1個字符的日期格式。
        字符串結果= now.ToString(“ s”);
        WriteLine($“ {dt} [s] = {result}”);
    }
}
輸出量

2020年11月9日12:04:17 [s] = 20

小時格式:

使用小時格式的C#程序

使用系統;

班級計劃
{
    靜態void Main()
    {
        //用13小時或1 PM創建DateTime。
        var result = new DateTime(2020,11,9,13,0,0);
        Console.WriteLine(“ HH:{0}”,result.ToString(“ HH”));
    }
}
輸出量

高度:13

日格式:對於幾天,我們使用一到四個d字符。一句話,“ d”和“ dd”表示月份,而“ ddd”和“ dddd”表示一周中的日子。

使用“ ddd”和“ dddd”的C#程序

使用系統;

班級計劃
{
    靜態void Main()
    {
        DateTime時間=新的DateTime(2020,9,11);
        // Console.WriteLine和string.Format可以處理日期。
        Console.WriteLine(“兩個字母:{0:ddd}”,時間);
        Console.WriteLine(“三個字母:{0:dddd}”,時間);
    }
}
輸出量

兩個字母:星期五
三個字母:星期五
整天:
顯示日期字符串的C#程序

使用系統;

班級計劃
{
    靜態void Main()
    {
        DateTime dt = DateTime.Today;
        對於(int i = 0; i <7; i ++)
        {
            Console.WriteLine(dt.ToString(“ dddd”));
            dt = dt.AddDays(1);
        }
    }
}
輸出量


星期五
星期六
星期日
星期一
星期二
星期三
星期四

三個字母的天:

使用系統;

班級計劃
{
    靜態void Main()
    {
        DateTime dt = DateTime.Today;
        對於(int i = 0; i <7; i ++)
        {
            Console.WriteLine(dt.ToString(“ ddd”));
            dt = dt.AddDays(1);
        }
    }
}
輸出量

週五
週六
太陽
週一
週二
星期三
週四

月格式:

使用月份格式的C#程序

使用系統;

班級計劃
{
    靜態void Main()
    {
        DateTime測試=新的DateTime(2020,9,9);
        Console.WriteLine(“ M:{0}”,test.ToString(“ M”)));
        Console.WriteLine(“ MM:{0}”,test.ToString(“ MM”));
        Console.WriteLine(“ MMM:{0}”,test.ToString(“ MMM”)));
        Console.WriteLine(“ MMMM:{0}”,test.ToString(“ MMMM”));
    }
}
輸出量

男:9月9日
MM:09
MMM:9月
MMMM:9月
時代。
打印當前時代的C#程序

使用系統;

班級計劃
{
    靜態void Main()
    {
        //我們處於公元時代。
        Console.WriteLine(DateTime.Now.ToString(“ gg”));
    }
}
輸出量

 

廣告。

AM,PM格式:指定一個t時,可以獲取AM或PM字符串的第一個字母。這等效於使用Substring或獲取tt字符串的第一個字符。


顯示AM和PM的C#程序

使用系統;

班級計劃
{
    靜態void Main()
    {
        DateTime dt = DateTime.Now;
        對於(int i = 0; i <2; i ++)
        {
            Console.WriteLine(dt.ToString(“ tt”));
            dt = dt.AddHours(12);
        }
    }
}
輸出量

下午
上午

年份格式:

注意:在幾乎所有程序中,年份將不需要三到五位數字,但是這些代碼存在。

顯示年份的C#程序

使用系統;

班級計劃
{
    靜態void Main()
    {
        DateTime現在= DateTime.Now;
        Console.WriteLine(now.ToString(“ y”));
        Console.WriteLine(now.ToString(“ yy”));
        Console.WriteLine(now.ToString(“ yyy”));
        Console.WriteLine(now.ToString(“ yyyy”));
        Console.WriteLine(now.ToString(“ yyyyy”));
    }
}
輸出量

0
20
2020年
2020年
02020

FormatException:

使用系統;

班級計劃
{
    靜態void Main()
    {
        const string dt =“ *”;
        字符串結果= DateTime.Today.ToString(dt);
    }
}
輸出量

未處理的異常:System.FormatException:輸入的字符串格式不正確。
   在System.DateTimeFormat.GetRealFormat ....
空值。當我們使用null,空或丟失的格式字符串格式化DateTime時,將獲得默認格式。如果我們沒有所需的特殊格式,則可以忽略該參數。
使用空,空,缺失格式的C#程序

使用系統;

班級計劃
{
    靜態void Main()
    {
        var dt = new DateTime(2000,9,11);
        Console.WriteLine(“ NULL格式字符串:{0}”,dt.ToString((string)null));
  Console.WriteLine(“ MISSING FORFORM STRING:{0}”,dt.ToString());
        Console.WriteLine(“空格式字符串:{0}”,dt.ToString(“”)));
    }
}
輸出量

空格式字符串:9/11/2020 12:00:00 AM
格式字符串丟失:2020年9月11日12:00:00 AM
空格式字符串:9/11/2020 12:00:00 AM

標籤:-
更改C#中的DateTime格式,更改C#中的日期格式,dateTime格式java,c#datetime格式yyyy-mm-dd,C#TimeSpan格式,有效DateTime格式C#,C#DateTime格式毫秒,DateTime C#

 

使用C#的日期時間格式
C# is a programming language developed by Microsoft that runs on the .NET Framework. C# is used to develop web, desktop, mobile, games and much more application. C# is a object-oriented programming language developed by Microsoft within its .NET Framework. Led by Anders Hejlsberg, your basic C# programming and will also take you through various advanced concepts related to C# programming language. C# such as control statements, objects and classes, inheritance, constructor, destructor, this, static, sealed, polymorphism, abstraction, abstract class, interface, File IO, Collections, namespace, encapsulation, properties, indexer, arrays, strings, regex, exception handling, multithreading etc. For example... using System; namespace MinifyCode { class Program { static void Main(string[] args) { Console.WriteLine("Hello Minify Code"); } } } Output: Hello Minify Code In this article you will learn, what is server side controls. We will discuss each of these objects in due time. In this tutorial we will explore the Server object, the Request object, and the Response object. Session Application Cache Request Response Server User Trace Server Object The Server object in Asp.NET is an instance of the System.Web.HttpServerUtility class. The HttpServerUtility class provides numerous properties and methods to perform many type of jobs. Methods and Properties of the Server object The methods and properties of the HttpServerUtility class are exposed through the intrinsic Server object provided by ASP.NET. using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Optimization; using System.Web.Routing; using System.Web.Security; using System.Web.SessionState; using System.Data.Entity; namespace minifycode { public class Global : HttpApplication { void Application_Start(object sender, EventArgs e) { // Code that runs on application startup RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); // Initialize the product database. Database.SetInitializer(new ProductDatabaseInitializer()); // Create custom role and user. RoleActions roleActions = new RoleActions(); roleActions.AddUserAndRole(); // Add Routes. RegisterCustomRoutes(RouteTable.Routes); } void RegisterCustomRoutes(RouteCollection routes) { routes.MapPageRoute( "ProductsCategoryRoute", "Category/{categoryName}", "~/ProductList.aspx" ); routes.MapPageRoute( "ProductNameRoute", "Product/{productName}", "~/ProductDetails.aspx" ); } } }