データベースからデータを取得して、GridView に時間を表示する必要があります。私はフォーマットの時間を持っています
2012-03-07 11:51:45.000
Gmailのように次のようにDateTimeを表示するにはどうすればよいですか?
今日の日付の場合、時刻が表示されます。または、日付をこの形式で表示する必要があります。
手伝って頂けますか?
DateTime Formattersを使用して、このようなことを行うことができます
public string ShowEmailAsGmail(DateTime dt)
{
DateTime now = DateTime.UtcNow;
if (dt.Date == now.Date)
return dt.ToString("hh:mm tt");
return dt.ToString("MMM dd");
}
これを次のようなカスタム オブジェクトのプロパティとして持つことができます。
public class EmailItem {
public DateTime SentDate { get; set; }
public string ShowEmailAsGmail { get {
{
DateTime now = DateTime.UtcNow;
if (this.SentDate.Date == now.Date)
return this.SentDate.ToString("HH:mm");
return this.SentDate.ToString("MMM dd");
}
}
を必要な形式にキャストする関数をコード ビハインドで作成し、DateTime
を使用してレンダリングしTemplateField
ます。
コード:
protected string GetCustomDateFormat(object dateTimeObj)
{
DateTime dateTime = (DateTime)dateTimeObj;
if (dateTime.Date == DateTime.Today)
{
return dateTime.ToString("hh:mm tt", CultureInfo.InvariantCulture);
//This Gives the Time in the Format (ex: 8:30 PM)
//return dateTime.ToShortTimeString();
// or you can specify format: dateTime.ToString("t")
}
else
{
return dateTime.ToShortDateString();
// or you can specify format: dateTime.ToString("m")
}
}
マークアップ:
<asp:GridView ID="GridView1" runat="server" ...
<Columns>
...
<asp:TemplateField HeaderText="Header text here">
<ItemTemplate>
<%# this.GetCustomDateFormat(Eval("DateTimeFieldName")) %>
</ItemTemplate>
</asp:TemplateField>
...
</Columns>
</asp:GridView>
DateTime time = DateTime.Now;
Console.WriteLine(time.ToString("d"));
Console.WriteLine(time.ToString("D"));
Console.WriteLine(time.ToString("f"));
Console.WriteLine(time.ToString("F"));
Console.WriteLine(time.ToString("g"));
Console.WriteLine(time.ToString("G"));
Console.WriteLine(time.ToString("m"));
Console.WriteLine(time.ToString("M"));
Console.WriteLine(time.ToString("o"));
Console.WriteLine(time.ToString("O"));
Console.WriteLine(time.ToString("s"));
Console.WriteLine(time.ToString("t"));
Console.WriteLine(time.ToString("T"));
Console.WriteLine(time.ToString("u"));
Console.WriteLine(time.ToString("U"));
Console.WriteLine(time.ToString("y"));
Console.WriteLine(time.ToString("Y"));
出力:-
d
2012年9月10日D2012年9月10日
月曜日f2012年9月10日月曜日12:11
PMF2012年9月10日月曜日12:12:22PMg2012年2月10日
12:12PMG
2 / 10/2012 12:12:22 PMm
9月
10M9月
10o2012-02-10T12:12:22.1020000-08:00
O 2012-02-10T12:12:22.1020000-08:00
s 2012-02-10T12: 12:22
t 12:12 PM
T 12:12:22 PM
u 2012-02-10 12:12:22ZU
2012年9月10日月曜日8:12:22PMy2012年
9月
Y2012年9月
次のチェックアウト
// create date time 2008-03-09 16:05:07.123
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);
String.Format("{0:y yy yyy yyyy}", dt); // "8 08 008 2008" year
String.Format("{0:M MM MMM MMMM}", dt); // "3 03 Mar March" month
String.Format("{0:d dd ddd dddd}", dt); // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}", dt); // "4 04 16 16" hour 12/24
String.Format("{0:m mm}", dt); // "5 05" minute
String.Format("{0:s ss}", dt); // "7 07" second
String.Format("{0:f ff fff ffff}", dt); // "1 12 123 1230" sec.fraction
String.Format("{0:F FF FFF FFFF}", dt); // "1 12 123 123" without zeroes
String.Format("{0:t tt}", dt); // "P PM" A.M. or P.M.
String.Format("{0:z zz zzz}", dt); // "-6 -06 -06:00" time zone
// date separator in german culture is "." (so "/" changes to ".")
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9/3/2008 16:05:07" - english (en-US)
String.Format("{0:d/M/yyyy HH:mm:ss}", dt); // "9.3.2008 16:05:07" - german (de-DE)
// month/day numbers without/with leading zeroes
String.Format("{0:M/d/yyyy}", dt); // "3/9/2008"
String.Format("{0:MM/dd/yyyy}", dt); // "03/09/2008"
// day/month names
String.Format("{0:ddd, MMM d, yyyy}", dt); // "Sun, Mar 9, 2008"
String.Format("{0:dddd, MMMM d, yyyy}", dt); // "Sunday, March 9, 2008"
// two/four digit year
String.Format("{0:MM/dd/yy}", dt); // "03/09/08"
String.Format("{0:MM/dd/yyyy}", dt); // "03/09/2008"
String.Format("{0:t}", dt); // "4:05 PM" ShortTime
String.Format("{0:d}", dt); // "3/9/2008" ShortDate
String.Format("{0:T}", dt); // "4:05:07 PM" LongTime
String.Format("{0:D}", dt); // "Sunday, March 09, 2008" LongDate
String.Format("{0:f}", dt); // "Sunday, March 09, 2008 4:05 PM" LongDate+ShortTime
String.Format("{0:F}", dt); // "Sunday, March 09, 2008 4:05:07 PM" FullDateTime
String.Format("{0:g}", dt); // "3/9/2008 4:05 PM" ShortDate+ShortTime
String.Format("{0:G}", dt); // "3/9/2008 4:05:07 PM" ShortDate+LongTime
String.Format("{0:m}", dt); // "March 09" MonthDay
String.Format("{0:y}", dt); // "March, 2008" YearMonth
String.Format("{0:r}", dt); // "Sun, 09 Mar 2008 16:05:07 GMT" RFC1123
String.Format("{0:s}", dt); // "2008-03-09T16:05:07" SortableDateTime
String.Format("{0:u}", dt); // "2008-03-09 16:05:07Z" UniversalSortableDateTime
日時フォーマッタを使用する必要があります。包括的なリスト (サンプルを含む) については、http: //msdn.microsoft.com/en-us/library/8kb3ddd4.aspxを参照してください。
どうですか:
if(myDate.Date == DateTime.Today)