Results 1 to 8 of 8

Thread: [RESOLVED] Time Stamp / Storage of Time in DB

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    46

    Resolved [RESOLVED] Time Stamp / Storage of Time in DB

    Hello all

    I Am storing time in a SQL db and right now the time is stored in HH.MM.SS
    I would like to only store the HH.MM.
    I know how to use the following to string

    DateTime.Now.ToString("MM/dd/yyyy hh:mm tt")

    but I am not storing in in String I storing in in a time field.

    I guess All i need to do is change the seconds to 00.

    ie 17:39:47 changed to 17:39:00

    Thanks

  2. #2

    Re: Time Stamp / Storage of Time in DB

    In the MSDN documentation for DateTime, you'll notice that one of the constructors (a way to create an object) takes the Year, Month, Day, Hour, Minute, and Second for the DateTime structure. Knowing this, you could easily, say, use the DateTime.Now property and create a new DateTime object with all the parameters set, except for second, which you would just pass 0 in.

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Time Stamp / Storage of Time in DB

    Are you storing the date as a string? Why not as a Date?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Re: Time Stamp / Storage of Time in DB

    Quote Originally Posted by dbasnett View Post
    Are you storing the date as a string? Why not as a Date?
    Silly dbasnett!

    but I am not storing in in String I storing in in a time field.

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Time Stamp / Storage of Time in DB

    Quote Originally Posted by formlesstree4 View Post
    Silly dbasnett!
    Ooops!
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    46

    Re: Time Stamp / Storage of Time in DB

    LOL with/ dbasnett

    OK
    I (being and vbdummy) am a bit confused at the MSDN info.


    My Punchtime comes in the form of a label displaying the time for the user.

    Code:
     
    Public Sub Timer1_tick(ByVal sender As Object, ByVal e As EventArgs)
            'on screen clock
            Label1.Text = Now.ToLongTimeString
        End Sub
    I then use the time from the label like this

    Code:
     Dim ShortPunchTime As TimeSpan = New TimeSpan(0, 0, 0, 0)
            Dim PunchTime As DateTime = DateTime.Parse(Label1.Text)
            ShortPunchTime = PunchTime.TimeOfDay
    I am then storing SHortPunchTime into A DB with a StoredProcedure into a time() field.




    ???

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    46

    Re: Time Stamp / Storage of Time in DB

    OK now its my turn to BEAT myself in the head for being "silly"

    now.toshorttimestring


    DUH

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Time Stamp / Storage of Time in DB

    That will work, but I really dislike the idea of taking a perfectly good date, turning it into a string, then turning that string back into a date. How about just having a date variable at form scope. Store the current time in the variable and just use the label to display the contents of the variable. Technically, it will matter very little, in this case, as there is only one less conversion by storing the date in a date variable, but I'd say that it's a good habit to avoid as many conversions from strings as you can.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width