Results 1 to 11 of 11

Thread: [RESOLVED] Server Time

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Posts
    129

    Resolved [RESOLVED] Server Time

    I want to capture the time when someone logs in the app that I'm making. Now we all have admin access so the person can change the system time and then login making the Date function kinda redundant. Is there a way I can get the given computer's time? I searched around the web and found couple of code bases but these are like way too long to get this. Is there a simple way to do this or another alternative instead of taking the server time?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Server Time

    This is pretty straight forward
    vb Code:
    1. Private Declare Function NetRemoteTOD Lib "NETAPI32.DLL"  _
    2. (ByVal server As String, _
    3. buffer As Any) As Long
    4.  
    5. Private Declare Function NetApiBufferFree Lib "NETAPI32.DLL"  _
    6. (ByVal buffer As Long) As Long
    7.  
    8. Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" _
    9. (hpvDest As Any, _
    10. hpvSource As Any, _
    11. ByVal cbCopy As Long)
    12.  
    13. Private Type TIME_OF_DAY
    14.     t_elapsedt As Long
    15.     t_msecs As Long
    16.     t_hours As Long
    17.     t_mins As Long
    18.     t_secs As Long
    19.     t_hunds As Long
    20.     t_timezone As Long
    21.     t_tinterval As Long
    22.     t_day As Long
    23.     t_month As Long
    24.     t_year As Long
    25.     t_weekday As Long
    26. End Type
    27.  
    28. Private Sub Command1_Click()
    29.     Dim t As TIME_OF_DAY
    30.     Dim tPtr As Long
    31.     Dim res As Long
    32.     Dim szServer As String
    33.     Dim days As Date
    34.     Dim todays As Date
    35.  
    36.    ' Replace \\ntsig with your NT server that you want to get the time from
    37.     szServer = StrConv("\\ntsig", vbUnicode)       'Convert the server name to unicode
    38.     res = NetRemoteTOD(szServer, tPtr)          'You could also pass vbNullString for the server name
    39.     If res = 0 Then
    40.         CopyMemory t, ByVal tPtr, Len(t)        'Copy the pointer returned to a TIME_OF_DAY structure
    41.         days = DateSerial(70, 1, 1) + (t.t_elapsedt / 60 / 60 / 24) 'Convert the elapsed time since 1/1/70 to a date
    42.         days = days - (t.t_timezone / 60 / 24)  'Adjust for TimeZone differences
    43.         'Get local computer information for comparison
    44.         todays = DateSerial(70, 1, 1) + (DateDiff("s", DateSerial(70, 1, 1), Now()) / 60 / 60 / 24)
    45.         Me.Cls
    46.         Print DateDiff("s", DateSerial(70, 1, 1), Now()), todays, t.t_elapsedt, days
    47.         NetApiBufferFree (tPtr)             'Free the memory at the pointer
    48.     Else
    49.         MsgBox "Error occurred Call NetRemoteTOD: " & res, vbOKOnly, "NetRemoteTOD"
    50.         'Error 53: cannot find server
    51.     End If
    52. End Sub

  3. #3

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Server Time

    BTW wussupbuddy.....what I posted and what RhinoBull posted is about as easy as it is going to get.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Posts
    129

    Re: Server Time

    Any other workaround? My backend DB is hosted on the server, so I created a table with a date field defaulting to Date(). The problem with this was till the time the record is not created I can't fetch this single record and once this is saved then it becomes static. Is there a way to fetch the default value for a field which is not created yet?

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Server Time

    Quote Originally Posted by wussupbuddy
    Any other workaround? My backend DB is hosted on the server, so I created a table with a date field defaulting to Date(). The problem with this was till the time the record is not created I can't fetch this single record and once this is saved then it becomes static. Is there a way to fetch the default value for a field which is not created yet?
    What does this have to do with getting the time on the server?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Posts
    129

    Re: Server Time

    Quote Originally Posted by Hack
    What does this have to do with getting the time on the server?
    My basic intention is to get current date..Thought of server time first sinve we can't change it..But since the code is so long, I thot I'll just get the Date() from the table hosted on the server.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Feb 2005
    Posts
    129

    Re: Server Time

    it's an access db btw...

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Server Time

    The posted code is NOT long at all.
    Quote Originally Posted by wussupbuddy
    Is there a way to fetch the default value for a field which is not created yet?
    Something which is not yet created does not have a value of any sort.

  10. #10
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Server Time

    If you are using SQL Server you can use GetDate Function:

    SQL Code:
    1. SELECT CONVERT(CHAR(26), GETDATE(), 109)
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  11. #11
    Hyperactive Member Philly0494's Avatar
    Join Date
    Apr 2008
    Posts
    485

    Re: Server Time

    you can make a vb program that prevents the user from changing the date and time as well as detecting when it does.

    its very simple to do

    to detect time change add a sysinfo control

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