0

I need to Display the Time In my GridView taking Data from Database. I have the Time in the Format

2012-03-07 11:51:45.000

How can I show the DateTime In the following way Like Gmail Does?

enter image description here

If it is Todays Date the Time Should be displayed. Or else The Date Should be Displayed in this Format.

Can you Help Me?

4

6 回答 6

3

You can use the DateTime Formatters to do something like this

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");
}

You could have this as a property of your custom object like:

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");
    }
}
于 2012-09-10T10:23:26.367 回答
2

Create a function in code behind that will cast your DateTime to the required format, and then render it using TemplateField.

Code:

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")
    }
}

Markup:

<asp:GridView ID="GridView1" runat="server" ...
    <Columns>
        ...
        <asp:TemplateField HeaderText="Header text here">
             <ItemTemplate>
                 <%# this.GetCustomDateFormat(Eval("DateTimeFieldName")) %>
             </ItemTemplate>
        </asp:TemplateField>
        ...
    </Columns>
</asp:GridView>
于 2012-09-10T10:30:50.870 回答
1
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"));

output:-

d 09/10/2012
D Monday, September 10, 2012
f Monday, September 10, 2012 12:11 PM
F Monday, September 10, 2012 12:12:22 PM
g 2/10/2012 12:12 PM
G 2/10/2012 12:12:22 PM
m September 10
M September 10
o 2012-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:22Z
U Monday, September 10, 2012 8:12:22 PM
y September, 2012
Y September, 2012

于 2012-09-10T10:36:07.597 回答
1

Checkout following

 // 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://www.dotnetperls.com/datetime-format

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

于 2012-09-10T11:07:28.750 回答
0

How about:

if(myDate.Date == DateTime.Today)
于 2012-09-10T10:21:33.357 回答
0

You should use date time formatters - for a comprehensive list (including samples), you can refer to: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

于 2012-09-10T10:26:43.417 回答