PDA

View Full Version : سوال: date time to string



rezaesmaeli
دوشنبه 07 آذر 1390, 20:45 عصر
تمامی حالتdate time to string را ذکر کنید؟؟ تقریبا 100حالت:گریه::گریه::گریه:

adineh67
دوشنبه 07 آذر 1390, 21:01 عصر
;()datevalue".tostring"

rezaesmaeli
دوشنبه 07 آذر 1390, 21:16 عصر
;()datevalue".tostring"
مثلا:dt.ToString("d");Dیک حالت"d" وبقیه حالتها می خواهم

Masterhame
پنج شنبه 10 آذر 1390, 15:30 عصر
سلام
به نظر من هر سوالی که دارید اول توی MSDNی که روی سیستمتون هست بگردید. اگه جوابتون پیدا نشد توی MSDN Online بگردید. حتما جوابتون رو پیدا می کنید. نهایتاً به این احتیاج پیدا می کنید که یه نفر مثالهایی که گیر آوردید رو براتون توضیح بده. من این کدها رو از توی MSDN استخراج کردم:

// This code example demonstrates the ToString(String) and
// ToString(String, IFormatProvider) methods for the DateTime
// type in conjunction with the standard date and time
// format specifiers.

using System;
using System.Globalization;
using System.Threading;

class Sample
{
public static void Main()
{
string msgShortDate = "(d) Short date: . . . . . . . ";
string msgLongDate = "(D) Long date:. . . . . . . . ";
string msgShortTime = "(t) Short time: . . . . . . . ";
string msgLongTime = "(T) Long time:. . . . . . . . ";
string msgFullDateShortTime =
"(f) Full date/short time: . . ";
string msgFullDateLongTime =
"(F) Full date/long time:. . . ";
string msgGeneralDateShortTime =
"(g) General date/short time:. ";
string msgGeneralDateLongTime =
"(G) General date/long time (default):\n" +
" . . . . . . . . . . . . . ";
string msgMonth = "(M) Month:. . . . . . . . . . ";
string msgRFC1123 = "(R) RFC1123:. . . . . . . . . ";
string msgSortable = "(s) Sortable: . . . . . . . . ";
string msgUniSortInvariant =
"(u) Universal sortable (invariant):\n" +
" . . . . . . . . . . . . . ";
string msgUniFull = "(U) Universal full date/time: ";
string msgYear = "(Y) Year: . . . . . . . . . . ";
string msgRoundtripLocal = "(o) Roundtrip (local):. . . . ";
string msgRoundtripUTC = "(o) Roundtrip (UTC):. . . . . ";
string msgRoundtripUnspecified = "(o) Roundtrip (Unspecified):. ";


string msg1 = "Use ToString(String) and the current thread culture.\n";
string msg2 = "Use ToString(String, IFormatProvider) and a specified culture.\n";
string msgCulture = "Culture:";
string msgThisDate = "This date and time: {0}\n";

DateTime thisDate = DateTime.Now;
DateTime utcDate = thisDate.ToUniversalTime();
DateTime unspecifiedDate = new DateTime(2000, 3, 20, 13, 2, 3, 0, DateTimeKind.Unspecified);
CultureInfo ci;

// Format the current date and time in various ways.
Console.Clear();
Console.WriteLine("Standard DateTime Format Specifiers:\n");
Console.WriteLine(msgThisDate, thisDate);
Console.WriteLine(msg1);

// Display the thread current culture, which is used to format the values.
ci = Thread.CurrentThread.CurrentCulture;
Console.WriteLine("{0,-30}{1}\n", msgCulture, ci.DisplayName);

Console.WriteLine(msgShortDate + thisDate.ToString("d"));
Console.WriteLine(msgLongDate + thisDate.ToString("D"));
Console.WriteLine(msgShortTime + thisDate.ToString("t"));
Console.WriteLine(msgLongTime + thisDate.ToString("T"));
Console.WriteLine(msgFullDateShortTime + thisDate.ToString("f"));
Console.WriteLine(msgFullDateLongTime + thisDate.ToString("F"));
Console.WriteLine(msgGeneralDateShortTime + thisDate.ToString("g"));
Console.WriteLine(msgGeneralDateLongTime + thisDate.ToString("G"));
Console.WriteLine(msgMonth + thisDate.ToString("M"));
Console.WriteLine(msgRFC1123 + utcDate.ToString("R"));
Console.WriteLine(msgSortable + thisDate.ToString("s"));
Console.WriteLine(msgUniSortInvariant + utcDate.ToString("u"));
Console.WriteLine(msgUniFull + thisDate.ToString("U"));
Console.WriteLine(msgYear + thisDate.ToString("Y"));
Console.WriteLine(msgRoundtripLocal + thisDate.ToString("o"));
Console.WriteLine(msgRoundtripUTC + utcDate.ToString("o"));
Console.WriteLine(msgRoundtripUnspecified + unspecifiedDate.ToString("o"));

Console.WriteLine();

// Display the same values using a CultureInfo object. The CultureInfo class
// implements IFormatProvider.
Console.WriteLine(msg2);

// Display the culture used to format the values.
ci = new CultureInfo("de-DE");
Console.WriteLine("{0,-30}{1}\n", msgCulture, ci.DisplayName);

Console.WriteLine(msgShortDate + thisDate.ToString("d", ci));
Console.WriteLine(msgLongDate + thisDate.ToString("D", ci));
Console.WriteLine(msgShortTime + thisDate.ToString("t", ci));
Console.WriteLine(msgLongTime + thisDate.ToString("T", ci));
Console.WriteLine(msgFullDateShortTime + thisDate.ToString("f", ci));
Console.WriteLine(msgFullDateLongTime + thisDate.ToString("F", ci));
Console.WriteLine(msgGeneralDateShortTime + thisDate.ToString("g", ci));
Console.WriteLine(msgGeneralDateLongTime + thisDate.ToString("G", ci));
Console.WriteLine(msgMonth + thisDate.ToString("M", ci));
Console.WriteLine(msgRFC1123 + utcDate.ToString("R", ci));
Console.WriteLine(msgSortable + thisDate.ToString("s", ci));
Console.WriteLine(msgUniSortInvariant + utcDate.ToString("u", ci));
Console.WriteLine(msgUniFull + thisDate.ToString("U", ci));
Console.WriteLine(msgYear + thisDate.ToString("Y", ci));
Console.WriteLine(msgRoundtripLocal + thisDate.ToString("o", ci));
Console.WriteLine(msgRoundtripUTC + utcDate.ToString("o", ci));
Console.WriteLine(msgRoundtripUnspecified + unspecifiedDate.ToString("o", ci));

Console.WriteLine();
}
}
/*
This code example produces the following results:

Standard DateTime Format Specifiers:

This date and time: 4/17/2006 2:22:48 PM

Use ToString(String) and the current thread culture.

Culture: English (United States)

(d) Short date: . . . . . . . 4/17/2006
(D) Long date:. . . . . . . . Monday, April 17, 2006
(t) Short time: . . . . . . . 2:22 PM
(T) Long time:. . . . . . . . 2:22:48 PM
(f) Full date/short time: . . Monday, April 17, 2006 2:22 PM
(F) Full date/long time:. . . Monday, April 17, 2006 2:22:48 PM
(g) General date/short time:. 4/17/2006 2:22 PM
(G) General date/long time (default):
. . . . . . . . . . . . . 4/17/2006 2:22:48 PM
(M) Month:. . . . . . . . . . April 17
(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:22:48 GMT
(s) Sortable: . . . . . . . . 2006-04-17T14:22:48
(u) Universal sortable (invariant):
. . . . . . . . . . . . . 2006-04-17 21:22:48Z
(U) Universal full date/time: Monday, April 17, 2006 9:22:48 PM
(Y) Year: . . . . . . . . . . April, 2006
(o) Roundtrip (local):. . . . 2006-04-17T14:22:48.2698750-07:00
(o) Roundtrip (UTC):. . . . . 2006-04-17T21:22:48.2698750Z
(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000

Use ToString(String, IFormatProvider) and a specified culture.

Culture: German (Germany)

(d) Short date: . . . . . . . 17.04.2006
(D) Long date:. . . . . . . . Montag, 17. April 2006
(t) Short time: . . . . . . . 14:22
(T) Long time:. . . . . . . . 14:22:48
(f) Full date/short time: . . Montag, 17. April 2006 14:22
(F) Full date/long time:. . . Montag, 17. April 2006 14:22:48
(g) General date/short time:. 17.04.2006 14:22
(G) General date/long time (default):
. . . . . . . . . . . . . 17.04.2006 14:22:48
(M) Month:. . . . . . . . . . 17 April
(R) RFC1123:. . . . . . . . . Mon, 17 Apr 2006 21:22:48 GMT
(s) Sortable: . . . . . . . . 2006-04-17T14:22:48
(u) Universal sortable (invariant):
. . . . . . . . . . . . . 2006-04-17 21:22:48Z
(U) Universal full date/time: Montag, 17. April 2006 21:22:48
(Y) Year: . . . . . . . . . . April 2006
(o) Roundtrip (local):. . . . 2006-04-17T14:22:48.2698750-07:00
(o) Roundtrip (UTC):. . . . . 2006-04-17T21:22:48.2698750Z
(o) Roundtrip (Unspecified):. 2000-03-20T13:02:03.0000000

*/

می تونید کامل کپی کنید و استفاده کنید.

در ضمن این قضیه که گفید بیش از 100 حالت مختلف هست، اینطور نیست. بلکه شما با توجه به سلیقه خودتون می تونید تعداد حروف و محل قرارگیری اونها رو توی رشته پیش فرض جابجا بکنید. به شکلی که مثلاً اول روز بعد سال بعد ماه! یا اینکه سال رو با دو عدد یا چهار عدد نشون بده.

یکسری از همین نمونه ها رو هم در ادامه اضافه کردم:


0 MM/dd/yyyy 08/22/2006
1 dddd, dd MMMM yyyy Tuesday, 22 August 2006
2 dddd, dd MMMM yyyy HH:mm Tuesday, 22 August 2006 06:30
3 dddd, dd MMMM yyyy hh:mm tt Tuesday, 22 August 2006 06:30 AM
4 dddd, dd MMMM yyyy H:mm Tuesday, 22 August 2006 6:30
5 dddd, dd MMMM yyyy h:mm tt Tuesday, 22 August 2006 6:30 AM
6 dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07
7 MM/dd/yyyy HH:mm 08/22/2006 06:30
8 MM/dd/yyyy hh:mm tt 08/22/2006 06:30 AM
9 MM/dd/yyyy H:mm 08/22/2006 6:30
10 MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM
10 MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM
10 MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM
11 MM/dd/yyyy HH:mm:ss 08/22/2006 06:30:07
12 MMMM dd August 22
13 MMMM dd August 22
14 yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2006-08-22T06:30:07.7199222-04:00
15 yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK 2006-08-22T06:30:07.7199222-04:00
16 ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Tue, 22 Aug 2006 06:30:07 GMT
17 ddd, dd MMM yyyy HH':'mm':'ss 'GMT' Tue, 22 Aug 2006 06:30:07 GMT
18 yyyy'-'MM'-'dd'T'HH':'mm':'ss 2006-08-22T06:30:07
19 HH:mm 06:30
20 hh:mm tt 06:30 AM
21 H:mm 6:30
22 h:mm tt 6:30 AM
23 HH:mm:ss 06:30:07
24 yyyy'-'MM'-'dd HH':'mm':'ss'Z' 2006-08-22 06:30:07Z
25 dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07
26 yyyy MMMM 2006 August
27 yyyy MMMM 2006 August