how to format a mysql time field to show as am or pm?

Rate It (1)

Last post 08-04-2008 3:15 AM by InterfaceMirror. 14 replies.

Sort Posts:

  • how to format a mysql time field to show as am or pm?

    07-14-2008, 6:10 PM
    • Member
      point Member
    • roi8877
    • Member since 07-14-2008, 9:42 PM
    • Posts 13

    Sorry if question has been asked already, but I was looking cannot find the appropriate answer....

    My Gridview display the data already but its displaying in 24 hour format.  

     

    I tried {0:t} but this works only on `datetime` data fields, does not appear to work on `time` fields.

     

    Any ideas? 

    Filed under: , , , ,
  • Re: how to format a mysql time field to show as am or pm?

    07-14-2008, 11:51 PM
    • All-Star
      94,406 point All-Star
    • vinz
    • Member since 10-05-2007, 11:47 AM
    • Cebu, PH
    • Posts 13,998
    • TrustedFriends-MVPs

    Try this

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DateTime dTime;
                if (DateTime.TryParse(e.Row.Cells[0].Text.Trim(),out dTime))
                {
                    TimeSpan tsTime = TimeSpan.Parse(dTime.ToString("HH:mm:ss"));
                    e.Row.Cells[0].Text = tsTime.ToString();
                }
            }
    }



    "Code,Beer and Music ~ my way of being a programmer"



  • Re: how to format a mysql time field to show as am or pm?

    07-15-2008, 12:28 AM
    • Member
      432 point Member
    • parthrawal
    • Member since 03-28-2008, 8:27 AM
    • Posts 535
    DateTime.ToString() Patterns 
    All the patterns: 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 
    
    The patterns for DateTime.ToString ( 'd' ) : 0	MM/dd/yyyy	08/22/2006 
    
    The patterns for DateTime.ToString ( 'D' ) : 0	dddd, dd MMMM yyyy	Tuesday, 22 August 2006 
    
    The patterns for DateTime.ToString ( 'f' ) : 0	dddd, dd MMMM yyyy HH:mm	Tuesday, 22 August 2006 06:30 
    1	dddd, dd MMMM yyyy hh:mm	tt Tuesday, 22 August 2006 06:30 AM 
    2	dddd, dd MMMM yyyy H:mm	Tuesday, 22 August 2006 6:30 
    3	dddd, dd MMMM yyyy h:mm	tt Tuesday, 22 August 2006 6:30 AM 
    
    The patterns for DateTime.ToString ( 'F' ) : 0	dddd, dd MMMM yyyy HH:mm:ss	Tuesday, 22 August 2006 06:30:07 
    
    The patterns for DateTime.ToString ( 'g' ) : 0	MM/dd/yyyy HH:mm	08/22/2006 06:30 
    1	MM/dd/yyyy hh:mm	tt 08/22/2006 06:30 AM 
    2	MM/dd/yyyy H:mm	08/22/2006 6:30 
    3	MM/dd/yyyy h:mm tt	08/22/2006 6:30 AM 
    
    The patterns for DateTime.ToString ( 'G' ) : 0	MM/dd/yyyy HH:mm:ss	08/22/2006 06:30:07 
    
    The patterns for DateTime.ToString ( 'm' ) : 0	MMMM dd	August 22 
    
    The patterns for DateTime.ToString ( 'r' ) : 0	ddd, dd MMM yyyy HH':'mm':'ss 'GMT'	Tue, 22 Aug 2006 06:30:07 GMT 
    
    The patterns for DateTime.ToString ( 's' ) : 0	yyyy'-'MM'-'dd'T'HH':'mm':'ss	2006-08-22T06:30:07 
    
    The patterns for DateTime.ToString ( 'u' ) : 0	yyyy'-'MM'-'dd HH':'mm':'ss'Z'	2006-08-22 06:30:07Z 
    
    The patterns for DateTime.ToString ( 'U' ) : 0	dddd, dd MMMM yyyy HH:mm:ss	Tuesday, 22 August 2006 06:30:07 
    
    The patterns for DateTime.ToString ( 'y' ) : 0	yyyy MMMM 2006 August

      

    Thanks
    Parth
    visit for some interesting articals at
    www.parthrawal.blogspot.com
    Mark as Answer if it helps you
  • Re: how to format a mysql time field to show as am or pm?

    07-15-2008, 12:49 PM
    • Member
      point Member
    • roi8877
    • Member since 07-14-2008, 9:42 PM
    • Posts 13

     Thanks vinz you for this. Where do I implement this? In the specific gridview load event? Just paste it there ? sorry bit newbie....

     

     

  • Re: how to format a mysql time field to show as am or pm?

    07-15-2008, 12:53 PM
    • Member
      point Member
    • roi8877
    • Member since 07-14-2008, 9:42 PM
    • Posts 13

    Thanks, I believe this would only work for datetime format...but not just a "time" field by itself.... a typical display for my time is 13:00:00 Id like to convert it to 1:00 PM .

    parthrawal:
    DateTime.ToString() Patterns All the patterns: 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
     

  • Re: how to format a mysql time field to show as am or pm?

    07-15-2008, 9:45 PM
    • All-Star
      94,406 point All-Star
    • vinz
    • Member since 10-05-2007, 11:47 AM
    • Cebu, PH
    • Posts 13,998
    • TrustedFriends-MVPs

    roi8877 :

    Thanks vinz you for this. Where do I implement this? In the specific gridview load event? Just paste it there ? sorry bit newbie....

    Place it under RowDataBound event of GridView since you are displaying your data in GridView.. Also i have some modifacation about my last post.. so try this below instead

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DateTime dTime;
                if (DateTime.TryParse(e.Row.Cells[0].Text.Trim(),out dTime)) //Just change the index of Cells to where your Time is being displayed in the GridView columns
                { 
                    e.Row.Cells[0].Text = dTime.ToString("h:mm tt");
                }
            }
    }

    Tested and it works fine in my part..

    Hope that helps..



    "Code,Beer and Music ~ my way of being a programmer"



  • Re: how to format a mysql time field to show as am or pm?

    07-16-2008, 12:10 AM
    • Member
      432 point Member
    • parthrawal
    • Member since 03-28-2008, 8:27 AM
    • Posts 535

     

    you just need to use format here....like for example  Format(STR_DATE,"HH:MM:SS").......

     

    Thanks
    Parth
    visit for some interesting articals at
    www.parthrawal.blogspot.com
    Mark as Answer if it helps you
  • Re: how to format a mysql time field to show as am or pm?

    07-16-2008, 12:21 AM
    • All-Star
      94,406 point All-Star
    • vinz
    • Member since 10-05-2007, 11:47 AM
    • Cebu, PH
    • Posts 13,998
    • TrustedFriends-MVPs

    parthrawal :

    you just need to use format here....like for example  Format(STR_DATE,"HH:MM:SS").......

    Hi parthrawal ,

    I don't think your code will not work..the OP wants to output 12 hour format with just hour, minutes with AM/PM..

    HH stands for 24 hour clock format

    MM stands for months and not minutes and basically will display numeric format

    SS  i dont know if it exist

     



    "Code,Beer and Music ~ my way of being a programmer"



  • Re: how to format a mysql time field to show as am or pm?

    07-16-2008, 12:26 AM
    • Member
      432 point Member
    • parthrawal
    • Member since 03-28-2008, 8:27 AM
    • Posts 535

     

    see my list of format.....in that i have put the formats like HH:MM:ss   and there are some other format also which will give the AM/PM format.....this is what i think,

    However you are far far more intelligent than me, so please bare with my ignorance if  i am saying something foolish

    Thanks
    Parth
    visit for some interesting articals at
    www.parthrawal.blogspot.com
    Mark as Answer if it helps you
  • Re: how to format a mysql time field to show as am or pm?

    07-16-2008, 12:34 AM
    • All-Star
      94,406 point All-Star
    • vinz
    • Member since 10-05-2007, 11:47 AM
    • Cebu, PH
    • Posts 13,998
    • TrustedFriends-MVPs

    parthrawal :

    see my list of format.....in that i have put the formats like HH:MM:ss

    Yes i saw it and there no no format like   HH:MM:ss.. maybe you mean this format  HH:mm:ss ..What i mean is that  MM and mm is different in values.. MM for Months and mm for minutes 

    parthrawal :

    However you are far far more intelligent than me, so please bare with my ignorance if  i am saying something foolish

    We are all equal here.. and please don't get it wrong.. I'm just trying to clarify about that things..

     



    "Code,Beer and Music ~ my way of being a programmer"



  • Re: how to format a mysql time field to show as am or pm?

    07-16-2008, 12:39 AM
    • Member
      432 point Member
    • parthrawal
    • Member since 03-28-2008, 8:27 AM
    • Posts 535

    Sorry, Here While writing for the post i forget that this is CASE Sensitive..............Really Sorry to misleading info.

    And really i am not taking this as an wrong thing, Sorry If these words feel Harsh.............Stick out tongue

    Thanks
    Parth
    visit for some interesting articals at
    www.parthrawal.blogspot.com
    Mark as Answer if it helps you
  • Re: how to format a mysql time field to show as am or pm?

    07-16-2008, 12:30 PM
    • Member
      point Member
    • roi8877
    • Member since 07-14-2008, 9:42 PM
    • Posts 13

    Great, So I got a chance this morning to implement this code. Im using visual web developer 2008. It seems like this code is written in a slight variation of the family of the microsoft programming lannguages... but i tried to rewrite it so it would work on VWD 2008.

     

    So far I have

    If (e.Row.RowType = DataControlRowType.DataRow) Then .....

    What does  DateTime dTime; mean? is dtime being declared as a data type of DateTime ? so id write is as dim dtime as datetime or what does it stand for?

     

    Thanks  

    vinz:

    Place it under RowDataBound event of GridView since you are displaying your data in GridView.. Also i have some modifacation about my last post.. so try this below instead

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

     
  • Re: how to format a mysql time field to show as am or pm?

    07-16-2008, 9:29 PM
    Answer
    • All-Star
      94,406 point All-Star
    • vinz
    • Member since 10-05-2007, 11:47 AM
    • Cebu, PH
    • Posts 13,998
    • TrustedFriends-MVPs

    roi8877:

    Great, So I got a chance this morning to implement this code. Im using visual web developer 2008. It seems like this code is written in a slight variation of the family of the microsoft programming lannguages... but i tried to rewrite it so it would work on VWD 2008.

    Basically the codes that I have provided was written in C#..So here's the VB.NET equivalent below

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim dTime As DateTime
            If DateTime.TryParse(e.Row.Cells(0).Text.Trim(), dTime) Then  'Just change the index of Cells to where your Time is being displayed in the GridView columns
                e.Row.Cells(0).Text = dTime.ToString("h:mm tt")
            End If
        End If
    End Sub 



    "Code,Beer and Music ~ my way of being a programmer"



  • Re: how to format a mysql time field to show as am or pm?

    07-18-2008, 2:43 PM
    • Member
      point Member
    • roi8877
    • Member since 07-14-2008, 9:42 PM
    • Posts 13

    Sweet, this works very well. ;) Thank you very much.

    Has given me insight on what one can do on the rowdatabound event.

    Also after a little more reading on the 'time' data type, one can use it to store time length, say 54 hours 20 minutes.... so Im thinking this may not be appropriate for my application.

    I should just use the datetime field, which would than make formating pretty easy.  (My web application is a meeting room bookings. So it has room name, start date, start time and end time and a few other fields) So you see i was using start time and end time as time fields....

     
    Thanks again! :) 

     

     

    vinz:

    Basically the codes that I have provided was written in C#..So here's the VB.NET equivalent below

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim dTime As DateTime
            If DateTime.TryParse(e.Row.Cells(0).Text.Trim(), dTime) Then  'Just change the index of Cells to where your Time is being displayed in the GridView columns
                e.Row.Cells(0).Text = dTime.ToString("h:mm tt")
            End If
        End If
    End Sub 

     
  • Re: how to format a mysql time field to show as am or pm?

    08-04-2008, 3:15 AM
    • Member
      147 point Member
    • InterfaceMirror
    • Member since 01-22-2006, 10:17 AM
    • Dubai, UAE
    • Posts 46

    Thanks,

     It is a very useful post. 

    http://geekswithblogs.net/interfacemirror
Page 1 of 1 (15 items)