ToShortDateString()

來源:互聯網
上載者:User

就是把你所操作的時間改變成短時間類型例如:2008/1/20 如果直接用tostring()則現實的時間是預設的格式
例如:2008-1-20 11:44:47
舉例:DateTime.Now.Date.ToShortDateString();
輸出:2008/1/20
如果DateTime.Now.Date.ToString();
2008-1-20 11:44:47

ToShortDateString 方法所返回的字串區分地區性。它反映目前範圍性的 DateTimeFormatInfo 對象所定義的模式。例如,對於 en-US 地區性,標準短日期模式為“M/d/yyyy”;對於 de-DE 地區性,該模式為“dd.MM.yyyy”;對於 ja-JP 地區性,該模式為“yyyy/M/d”。此外,也可以在特定電腦上自訂特定格式字串,以便該字串不同於標準短日期格式字串。

日期用ToShortDateString格式化,為什麼換了機器以後得出來的不是xxxx-xx-x的形式,而是x/xx/xxxx?

只要在控制台中的"地區和語言選項"設定"地區選項"就行了

string d1 = DateTime.Now.Date.ToShortDateString();//2004-8-12  
  string d2 = DateTime.Now.ToShortTimeString();//16:42  
  string d3 = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");//2004-08-12 04:42:34  
  string d4 = DateTime.Now.ToString("yyyy-MM-dd");//2004-08-12  
  string d5 = DateTime.Now.ToString("HH:mm:ss");//16:42:41

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.