1

这是我能写的吗?

string text = string.Format("{0:d/MM/yy}", DateTime.Now.Date);
text = text.Replace("-", "");
char[] cString = text.ToCharArray();
string year = text.Substring(text.Length - 2);
string month = cString[2].ToString() + cString[3].ToString();
string day = cString[0].ToString() + cString[1].ToString();

请帮我找出错误,因为它没有在我的 Win Form Application 上正确显示。它显示 01 05 13 为 10 __ 13。

4

3 回答 3

8
DateTime today = DateTime.Now;
int year = today.Year;
int month = today.Month
int day = today.Day;
于 2013-05-01T19:26:49.413 回答
5

你可以这样做:

var date = DateTime.Now.Date;
var parts = date.ToString("d MM yy").Split(" "); // dd MM yy for 2-digit day
var year = parts[2];
var month = parts[1];
var day = parts[0];

但这似乎更容易:

var date = DateTime.Now.Date;
var year = date.ToString("yy");
var month = date.ToString("MM");
var day = date.ToString("d");  // dd for 2-digit day
于 2013-05-01T19:26:16.433 回答
0

你不能这样做:

DateTime currDate = DateTime.Now;

string year = currDate.ToString("yy");
string month = currDate.ToString("MM");
string day  currDate.ToString("d");

要不就

string date = DateTime.Now.ToString("d MM yy"); 
于 2013-05-01T19:28:41.630 回答