0

I am trying to develop an application, where it wont rely on the current system time;

Even when I change the date and time on my PC/System, it wont change.

MarmiK
  • 5,639
  • 6
  • 40
  • 49
  • Could you please clarify what do you use that time for? For example, do you want to run some code every 5 minutes? Or do you want to wait a second? – wigy Aug 20 '15 at 07:30
  • Sorry for the confusion i've edited the title thankyou for the reply @wigy – Robin Bernardo Aug 20 '15 at 07:40
  • The time/date is for transaction for the database. To follow the SET time of the application not the Date/time of the Current PC Date/Time @wigy – Robin Bernardo Aug 20 '15 at 07:43
  • if you have intenet in your system, you can check the `time server`(any you like, as many available for free) then use UTC DateTime. as that will stay the same even if the user region changes! – MarmiK Aug 20 '15 at 07:44
  • Check this StackOverflow post: http://stackoverflow.com/questions/8867124/using-vb-net-to-query-ntp-servers – joehanna Aug 20 '15 at 07:45
  • Fragment revised with minor correction – MarmiK Aug 20 '15 at 07:45
  • The client does not have internet connection and I dont want to rely on the current PC's Time. Is it possible to set/create the date/time for this application ? @MarmiK – Robin Bernardo Aug 20 '15 at 07:50
  • Always check the system's `last entry time` with `current entry time`, which(current entry time) should be always higher.. else ask/alert for system time has changed "Please correct time!"; FYI.. this logic was used by old banking and accounting system with stand alone system to overcome the bios battery dead and giving default time `01-01-1900` :) hope this helps – MarmiK Aug 20 '15 at 07:53
  • Yes! im getting the idea Thankyou! but, Where will i input the date/time, In the Pc' Time or in the Application time ? @MarmiK – Robin Bernardo Aug 20 '15 at 07:57
  • In application you have to record PC time initially when you install, then application will/should automatically update time every time the application closes(as last use time), use one variable or config file or database, what ever suits you! – MarmiK Aug 20 '15 at 07:59
  • Thankyou Appreciated the help! @MarmiK – Robin Bernardo Aug 20 '15 at 08:08
  • @RobinBernardo most welcome, please upvote the useful comment to make it easy for later readers to identify useful comment, and also you can post your own answer that finally works for you.. – MarmiK Aug 20 '15 at 08:12

1 Answers1

1

Unfortunately if you want to measure time intervals, you need to choose a clock as a reference. Reading your other question and your comments, I get the impression that you just want to know how much time elapsed since a given time instance.

If you just worry about the daylight changing in spring and autumn, you should just use the system time as UTC:

Public StartDate As Date = Date.UtcNow
...
Dim elapsed As TimeSpan = Date.UtcNow - StartDate

If you also want to ignore when a system administrator changes the system time, you need to rely on the high-precision counter of the CPU (although that cannot be used to measure time between different runs of your program):

Imports System.Diagnostics
...
Dim myClock As New StopWatch()
myClock.Start()
...
Dim elapsed As TimeSpan = myClock.Elapsed

And if you would like to use the system time on the database server, you should query that:

Dim query As String = "SELECT GETDATE()"

Using connection As New SqlConnection("your connection string here"), _
  cmd As New SqlCommand(query, connection)
    connectionn.Open()
    Dim timeOnSqlServer As Date = Convert.ToDateTime(cmd.ExecuteScalar())
End Using
Community
  • 1
  • 1
wigy
  • 2,174
  • 19
  • 32