Display current datetime depending on client [duplicate] - c#

This question already has answers here:
How to handle Azure's UTC time
(2 answers)
Closed 8 years ago.
I am currently saving the datetime.utcnow in the database on azure but im not sure how to display the correct time depending on the client.
Should I be using utc?
Should I save the timezone so I can recalculate the time?
Can I use the culture to change the time to the client time?

Your best option is to store the DateTime as UTC time and adjust it to local time (for the user's current location) each time you read it from the database.
You can use JavaScript to determine the timezone of the user:
new Date().gettimezoneOffset()

Related

Is comparing DateTime saved accross different systems a Bad idea? [duplicate]

This question already has answers here:
Compare DateTime ticks on two machines
(5 answers)
Closed 3 years ago.
In one of my code I am doing so that, there is a process running different systems and I save some data to database with that I saved field DateTime with value DateTime.UtcNow (C#) , and then one of the systems I collect all the data from different systems then order it by DateTime field, to order the data has been saved.
In this I also check if SystemA written data DateTime field is greater than SystemB written data DateTime field and then which ever data is latest I save that.
Now, I would like to know whether this kind of DateTime comparision from different systems is a bad idea?
I am not considering for scenarios where the system clock are very wrong.
The local clock on a PC is not that accurate. Its entirely possible that comparing two timestamps between systems will result in incorrect results. Instead have the database apply the timestamp from its own server as it writes the record.

C# - DateTime.Now.TimeOfDay is different on my other comptuter by 2 seconds, how do I get accurate time? [duplicate]

This question already has answers here:
How to Query an NTP Server using C#?
(6 answers)
Closed 3 years ago.
I need to get the same time on all Instances of my program. How do I do that. DateTime.Now is not accurate(is different on different hardware) enough, I need to get the Time down to 100 ms difference-precision.
You don't want it read locally from each computer, as you don't know that each PC's clock is perfectly in sync (and you can see that they aren't).
So you have two options:
Write something on each computer to maintain precise time.
Get a web based time and all of your computers will be reading the same data. Here is an example from SO: How to get DateTime from the internet?

C# getting the date [duplicate]

This question already has answers here:
How to remove time portion of date in C# in DateTime object only?
(43 answers)
convert string to date without time
(3 answers)
Closed 5 years ago.
I'm converting a string to a date format using DateTime.Parse(). My original string consists of the date only but when i use DateTime.Parse() it adds the time to it as well giving me 01/12/2000 00:00:00. I only want the date 01/12/2000. Is there any other way to simply just get the date?
DateTime dt = DateTime.Parse("01/12/2000");
Console.WriteLine(dt.ToString("dd/MM/yyyy"));
DateTime always has a underlying Time fraction, it's just the way you define to show it that makes it look like that. So the tostring function can be used with the given formatting.
If you really want it not to have a time (and i wouldn't know why you'd want that) there are 3th party addons available.

Detect Timezone of client using c# [duplicate]

This question already has answers here:
How to get client date and time in ASP.NET?
(6 answers)
How to detect the timezone of a client?
(4 answers)
Closed 6 years ago.
I am using ASP.NET Webforms to get the data of client. till now I figured out the IP address of client and the city & country of the client. I am trying to find a way of how to retrieve the time zone by using its IP Address or country name or city name. I have googled and couldn't find any appropriate answer as everyone talks about injecting javascript to get timezone.
My question is: Is there any way to find timezone of the client? If so then how?`
It is not possible to do this in a reliable way purely with C#. The HTTP spec does not provide any hints of the client's time zone.
You can guess at a client's time zone by using IP geolocation, and there are many free and commercial services that provide time zone information. However there is only a limited degree of accuracy with such an approach.
Even with JavaScript, the current solutions can still only guess the client's time zone. There are improvements happening for this with in the ECMAScript Intl API, but not all browsers fully implement that yet. See more here.
Ultimately, if you need to know the time zone of the client from your back end code, the best thing to do is to ask the user. Provide a time zone picker somewhere in your application.
You may also find this related answer useful.
Have you tried using the TimeZoneInfo Class
It represents any time zone in the world.
https://msdn.microsoft.com/en-us/library/system.timezoneinfo(v=vs.110).aspx

Update system time and date from internet [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to Query an NTP Server from C#
I try send email with my program but if time and date not set, sending is be fail.
How to update system time and date from internet in C#?
The answers to this question show you how to retrieve the time from a time server.
Are you sure you want to update the system time? Maybe it is sufficient to use the retrieved time in the email.

Categories