Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Easy DateTime question

Easy DateTime question

Scheduled Pinned Locked Moved Database
questiondatabasehelptutorial
36 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gantww
    wrote on last edited by
    #1

    I know that this can't be too hard, but for whatever reason, I haven't been able to figure out how to do it. I have a query that is returning a group of fields, including some date fields. The data in the field is formatted as follows: 2007-02-08 00:00:00.000 The date part is fine, but I need the time to be standard time (hour:minute:second:millisecond AM/PM) instead of military because of a brittle application that reads the data. Can anybody help me?

    K 1 Reply Last reply
    0
    • G gantww

      I know that this can't be too hard, but for whatever reason, I haven't been able to figure out how to do it. I have a query that is returning a group of fields, including some date fields. The data in the field is formatted as follows: 2007-02-08 00:00:00.000 The date part is fine, but I need the time to be standard time (hour:minute:second:millisecond AM/PM) instead of military because of a brittle application that reads the data. Can anybody help me?

      K Offline
      K Offline
      kubben
      wrote on last edited by
      #2

      select convert(varchar(28),@dt,131) Where @dt is your datetime variable. Ben

      G B 2 Replies Last reply
      0
      • K kubben

        select convert(varchar(28),@dt,131) Where @dt is your datetime variable. Ben

        G Offline
        G Offline
        gantww
        wrote on last edited by
        #3

        That's odd. Now it's coming out as: 3/02/1428 12:00:00:000AM Now the time is right, but the date is odd. The offset seems about right for the Arabic year though (if I remember my Middle East history).

        K 1 Reply Last reply
        0
        • G gantww

          That's odd. Now it's coming out as: 3/02/1428 12:00:00:000AM Now the time is right, but the date is odd. The offset seems about right for the Arabic year though (if I remember my Middle East history).

          K Offline
          K Offline
          kubben
          wrote on last edited by
          #4

          Sorry I didn't look close enough to the format it puts it in: dd/mm/yy hh:mi:ss:mmmAM Here is what I know: Change the 131 to: 100 you get mon dd yyyy hh:miAM (or PM) 109 you get mon dd yyyy hh:mi:ss:mmmAM (or PM) So then your date isn't formatted right. It is almost starting to look like you need to take the date part that is ok and then take the time part from 109 Anyway it is a bit more of a pain, but if you need that specific format here it is: declare @dt datetime, @time varchar(28), @date varchar(30) select @time = convert(varchar(28),@dt,109) select @date = convert(varchar(30),@dt,112) Select substring(@date,1,4)+'-'+substring(@date,5,2)+'-'+Substring(@date,7,2)+' '+ltrim(substring(@time,12,15)) Ben

          1 Reply Last reply
          0
          • K kubben

            select convert(varchar(28),@dt,131) Where @dt is your datetime variable. Ben

            B Offline
            B Offline
            bernie_011
            wrote on last edited by
            #5

            idol... i have 3 columns in my table the room,timeStart,timeEnd. For example i have inserted this data room: room1 timeStart: 2007-03-02 8:00 AM timeEnd: 2007-03-02 3:00 PM Here's the data that will not be accepted if try to insert room: room1 timeStart: 2007-03-02 9:00 AM timeEnd: 2007-03-02 4:00 PM because the timestart is between the timeStart and the timeEnd. Room1 will only be occupied after 3:00 PM. Can you help me with this?. Thank you very Much Bernie

            K 1 Reply Last reply
            0
            • B bernie_011

              idol... i have 3 columns in my table the room,timeStart,timeEnd. For example i have inserted this data room: room1 timeStart: 2007-03-02 8:00 AM timeEnd: 2007-03-02 3:00 PM Here's the data that will not be accepted if try to insert room: room1 timeStart: 2007-03-02 9:00 AM timeEnd: 2007-03-02 4:00 PM because the timestart is between the timeStart and the timeEnd. Room1 will only be occupied after 3:00 PM. Can you help me with this?. Thank you very Much Bernie

              K Offline
              K Offline
              kubben
              wrote on last edited by
              #6

              I don't know if there is a very good way of doing this. No matter which way you do it, it will be a lot of work. Most likely you will need to have some table that keeps track of when each class can be in each room and then check that table before inserting the record. Good luck. Ben

              B 1 Reply Last reply
              0
              • K kubben

                I don't know if there is a very good way of doing this. No matter which way you do it, it will be a lot of work. Most likely you will need to have some table that keeps track of when each class can be in each room and then check that table before inserting the record. Good luck. Ben

                B Offline
                B Offline
                bernie_011
                wrote on last edited by
                #7

                Can you tell me how to use the check Constraint for that? Bernie

                K 1 Reply Last reply
                0
                • B bernie_011

                  Can you tell me how to use the check Constraint for that? Bernie

                  K Offline
                  K Offline
                  kubben
                  wrote on last edited by
                  #8

                  I have never used a check constraint in the way you are wanting to use it. I am not sure it can be done. If all records getting inserted had a certain rule then a check constraint would be the correct one to use. I am under the impression that each record might have a different rule. In that case I would not use the check constraint. Ben

                  B 1 Reply Last reply
                  0
                  • K kubben

                    I have never used a check constraint in the way you are wanting to use it. I am not sure it can be done. If all records getting inserted had a certain rule then a check constraint would be the correct one to use. I am under the impression that each record might have a different rule. In that case I would not use the check constraint. Ben

                    B Offline
                    B Offline
                    bernie_011
                    wrote on last edited by
                    #9

                    ok can you teach me how to solved that? I dont know how to start my code for that. Bernie

                    K 1 Reply Last reply
                    0
                    • B bernie_011

                      ok can you teach me how to solved that? I dont know how to start my code for that. Bernie

                      K Offline
                      K Offline
                      kubben
                      wrote on last edited by
                      #10

                      It is going to be a complicated solution. I think you should just try your best and figure it out any way you can. Ben

                      B 1 Reply Last reply
                      0
                      • K kubben

                        It is going to be a complicated solution. I think you should just try your best and figure it out any way you can. Ben

                        B Offline
                        B Offline
                        bernie_011
                        wrote on last edited by
                        #11

                        ok.. i will..thanks for that. I have another question if ok. you are so blessed ben, How about rowfilter using textbox and dropdown?. Bernie

                        K 1 Reply Last reply
                        0
                        • B bernie_011

                          ok.. i will..thanks for that. I have another question if ok. you are so blessed ben, How about rowfilter using textbox and dropdown?. Bernie

                          K Offline
                          K Offline
                          kubben
                          wrote on last edited by
                          #12

                          The rowfilter you can use in a DataView. I can't remeber if you do vb.net or C# but it is something like this: DataView dv = yourdatatable.DefaultView; dv.RowFilter = "columname = "+textbox.Text; gridview.datasource = dv; gridview.databind(); Anyway, that is what the C# looks like. Ben

                          B 1 Reply Last reply
                          0
                          • K kubben

                            The rowfilter you can use in a DataView. I can't remeber if you do vb.net or C# but it is something like this: DataView dv = yourdatatable.DefaultView; dv.RowFilter = "columname = "+textbox.Text; gridview.datasource = dv; gridview.databind(); Anyway, that is what the C# looks like. Ben

                            B Offline
                            B Offline
                            bernie_011
                            wrote on last edited by
                            #13

                            i got the answer Ben, just now. I just configure it in SQL data source. thank you very much. I am just a beginner Ben thats why i am asking. Can i have another question?..Hehe How about computing difference of two time to be display in textbox before inserting Bernie

                            K 1 Reply Last reply
                            0
                            • B bernie_011

                              i got the answer Ben, just now. I just configure it in SQL data source. thank you very much. I am just a beginner Ben thats why i am asking. Can i have another question?..Hehe How about computing difference of two time to be display in textbox before inserting Bernie

                              K Offline
                              K Offline
                              kubben
                              wrote on last edited by
                              #14

                              Here's a link that might help you with this. ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref2/html/M_System_DateTime_Subtract_1_4910510e.htm Ben

                              B 1 Reply Last reply
                              0
                              • K kubben

                                Here's a link that might help you with this. ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref2/html/M_System_DateTime_Subtract_1_4910510e.htm Ben

                                B Offline
                                B Offline
                                bernie_011
                                wrote on last edited by
                                #15

                                i cant access the link..no page display..and another question how can i validate time input hh:mm:ss using range validator, and how can i set the datetime.now in textbox gridview in edit mode. Thanks Bernie

                                K 2 Replies Last reply
                                0
                                • B bernie_011

                                  i cant access the link..no page display..and another question how can i validate time input hh:mm:ss using range validator, and how can i set the datetime.now in textbox gridview in edit mode. Thanks Bernie

                                  K Offline
                                  K Offline
                                  kubben
                                  wrote on last edited by
                                  #16

                                  Here is the content of the link: Subtracts the specified date and time from this instance. Namespace: System Assembly: mscorlib (in mscorlib.dll) Syntax Visual Basic (Declaration) Public Function Subtract ( _ value As DateTime _ ) As TimeSpan Visual Basic (Usage) Dim instance As DateTime Dim value As DateTime Dim returnValue As TimeSpan returnValue = instance.Subtract(value) C# public TimeSpan Subtract ( DateTime value ) C++ public: TimeSpan Subtract ( DateTime value ) J# public TimeSpan Subtract ( DateTime value ) JScript public function Subtract ( value : DateTime ) : TimeSpan Parameters value An instance of DateTime. Return Value A TimeSpan interval equal to the date and time represented by this instance minus the date and time represented by value. Exceptions Exception type Condition ArgumentOutOfRangeException The result is less than MinValue or greater than MaxValue. Remarks This method does not change the value of this DateTime object. Instead, a new TimeSpan is returned whose value is the result of this operation. Before subtracting DateTime objects, insure that the objects represent times in the same time zone. Otherwise, the result will include the difference between time zones. Example The following code example demonstrates the Subtract method and operator. Visual Basic Copy Code Dim date1 As New System.DateTime(1996, 6, 3, 22, 15, 0) Dim date2 As New System.DateTime(1996, 12, 6, 13, 2, 0) Dim date3 As New System.DateTime(1996, 10, 12, 8, 42, 0) Dim diff1 As System.TimeSpan ' diff1 gets 185 days, 14 hours, and 47 minutes. diff1 = date2.Subtract(date1) Dim date4 As System.DateTime ' date4 gets 4/9/1996 5:55:00 PM. date4 = date3.Subtract(diff1) Dim diff2 As System.TimeSpan ' diff2 gets 55 days 4 hours and 20 minutes. diff2 = System.DateTime.op_Subtraction(date2, date3) Dim date5 As System.DateTime ' date5 gets 4/9/1996 5:55:00 PM. date5 = System.DateTime.op_Subtraction(date1, diff2) Ben

                                  B 1 Reply Last reply
                                  0
                                  • B bernie_011

                                    i cant access the link..no page display..and another question how can i validate time input hh:mm:ss using range validator, and how can i set the datetime.now in textbox gridview in edit mode. Thanks Bernie

                                    K Offline
                                    K Offline
                                    kubben
                                    wrote on last edited by
                                    #17

                                    You can not validate time input with a range validator. It will only validate the date part. In your gridview you need an EditItemTemplate. In that template you need a textbox. The code would look like: yourtxtbox.Text='<%# DateTime.Now %>' Ben

                                    1 Reply Last reply
                                    0
                                    • K kubben

                                      Here is the content of the link: Subtracts the specified date and time from this instance. Namespace: System Assembly: mscorlib (in mscorlib.dll) Syntax Visual Basic (Declaration) Public Function Subtract ( _ value As DateTime _ ) As TimeSpan Visual Basic (Usage) Dim instance As DateTime Dim value As DateTime Dim returnValue As TimeSpan returnValue = instance.Subtract(value) C# public TimeSpan Subtract ( DateTime value ) C++ public: TimeSpan Subtract ( DateTime value ) J# public TimeSpan Subtract ( DateTime value ) JScript public function Subtract ( value : DateTime ) : TimeSpan Parameters value An instance of DateTime. Return Value A TimeSpan interval equal to the date and time represented by this instance minus the date and time represented by value. Exceptions Exception type Condition ArgumentOutOfRangeException The result is less than MinValue or greater than MaxValue. Remarks This method does not change the value of this DateTime object. Instead, a new TimeSpan is returned whose value is the result of this operation. Before subtracting DateTime objects, insure that the objects represent times in the same time zone. Otherwise, the result will include the difference between time zones. Example The following code example demonstrates the Subtract method and operator. Visual Basic Copy Code Dim date1 As New System.DateTime(1996, 6, 3, 22, 15, 0) Dim date2 As New System.DateTime(1996, 12, 6, 13, 2, 0) Dim date3 As New System.DateTime(1996, 10, 12, 8, 42, 0) Dim diff1 As System.TimeSpan ' diff1 gets 185 days, 14 hours, and 47 minutes. diff1 = date2.Subtract(date1) Dim date4 As System.DateTime ' date4 gets 4/9/1996 5:55:00 PM. date4 = date3.Subtract(diff1) Dim diff2 As System.TimeSpan ' diff2 gets 55 days 4 hours and 20 minutes. diff2 = System.DateTime.op_Subtraction(date2, date3) Dim date5 As System.DateTime ' date5 gets 4/9/1996 5:55:00 PM. date5 = System.DateTime.op_Subtraction(date1, diff2) Ben

                                      B Offline
                                      B Offline
                                      bernie_011
                                      wrote on last edited by
                                      #18

                                      thanks for that friend, I got it now. I have new question, how can i add gridview header above header?.. Thanks Bernie

                                      K 1 Reply Last reply
                                      0
                                      • B bernie_011

                                        thanks for that friend, I got it now. I have new question, how can i add gridview header above header?.. Thanks Bernie

                                        K Offline
                                        K Offline
                                        kubben
                                        wrote on last edited by
                                        #19

                                        You can try to use the gridview pager template for that sort of thing. Ben

                                        B 1 Reply Last reply
                                        0
                                        • K kubben

                                          You can try to use the gridview pager template for that sort of thing. Ben

                                          B Offline
                                          B Offline
                                          bernie_011
                                          wrote on last edited by
                                          #20

                                          What are the settings to be change for me to show the header on load?. I found out that it depends on the number of rows. Thanks bernie

                                          K 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups