0

How can i change my device/pc time using C#?

Or if not possible can i atleast get the SQL's server time and use it as a basis in a ticking time. Im doing a daily time record system. Help me thanks

Chddz
  • 15
  • 4
  • 1
    Why do you want to change your device time? Why do you want to get the SQL server's time? If you're storing the data in SQL server, can't you just use `GETUTCDATE()` in SQL? – ProgrammingLlama Oct 10 '17 at 01:18
  • Cause, Ill be implementing the system at a lot of pc, So even the pc's time arent update. It'll just retrieve the updated time from the server. so the date time record system will be accurate in registering login/logout of employees. – Chddz Oct 10 '17 at 01:22
  • If you are using a server / service have it store and use the times, in UTC and go from that. Client times shouldn't really matter – Austin T French Oct 10 '17 at 01:25
  • @Chddz - I worked on a time tracking system years ago and at the same time we were doing Y2K testing so we couldn't synchronize all of the machines to the same time. The way we did it was to use a single time server that the application that all of the clients used to get the "current" time. You could easily do something like that by using Web Api as a time server or you could use an [NTP server](https://stackoverflow.com/a/12150289/). – NightOwl888 Oct 10 '17 at 01:25
  • The best way to handle this is to use DateTime.Utc instead of worrying about settings of machines and servers – Chetan Oct 10 '17 at 01:25
  • @ChetanRanpariya that isn't really the ***best*** way if time accuracy is very important. Client times can be off wildly for various reasons (bad CMOS battery, users changing their times) – Austin T French Oct 10 '17 at 01:34
  • @Austin if date and time are stored in utc the there is no need to worry about what time zone it is in. At the time of display only it needs to be converted to local machine time zone so that user can relate it. If stored time zone of each and every date then it becomes difficult to target time zone while displaying and also generating reports and reading them becomes much more simpler with UTC times. And storing in UTC doesn't loose accuracy. – Chetan Oct 10 '17 at 01:40
  • @ChetanRanpariya we may be saying the exact same thing then. I read your first comment as having the client do a DateTime.Now.ToUTC(); If the server is doing the generation and storing (my earlier comment) then yeah I probably wouldn't worry about it – Austin T French Oct 10 '17 at 01:44
  • Yes... You are right.. If server handles the date time storage then client settings are irrelevant. I hope this helps OP make good decision – Chetan Oct 10 '17 at 01:50
  • My Client said, By doing this process.. I should only connect at SQL once.. Like, Once I retrieve the servertime, thats it no more retrieving time at sql.. – Chddz Oct 10 '17 at 01:52
  • Can you explain what exactly is the use case here? Why sql server time is so important here? – Chetan Oct 10 '17 at 01:53
  • @ChetanRanpariya Because, this is a Date Time Record. And the login/logout of an employee must be accurate and based on sql server time. Because their login and logout will be the basis of their payment slip. – Chddz Oct 10 '17 at 01:55
  • @Chddz But where are you saving these time records? Not also in SQL Server? – ProgrammingLlama Oct 10 '17 at 05:10

1 Answers1

0

Solution for you is use protocol for synchronize time between computers over packet switching... for that i will suggest you to use this Simple Network Time Protocol, with that you can retrive datetime from server and do your work...

another example with that http://dotnet-snippets.com/snippet/simple-network-time-ntp-protocol-client/571