Datetime.Now incorrectly determining my location - c#

I have a website that displays the current date using the code Datetime.Now. From what I understand, Datetime.Now is supposed to display the current time as it would appear in the viewer's current locale.
At the moment, when I test on localhost, the website is correctly determining my location (en-nz) and displaying the right date. However, when I run the site live, I'm getting a different date all together.
So how does a system determine a viewer's locale and why is there a difference between how my site is displaying Datetime.Now locally and live?

DateTime.Now has the time of the server, but not the time of the client. If you want to detect the time of the client you need to use Javascript, either by detecting the time with it or calculating it using the time zones.
In your localhost works fine, since the server and the client are in the same PC/Time Zone
Hope it helps!

No, DateTime.Now will retrieve the current time in the local time zone of the machine it's running on. In other words, the web server in your case, assuming you're writing a web app (you haven't made it clear).
If you want to display it in the local time zone of a browser, you may well be best to send down DateTime.UtcNow and write some JavaScript to convert that to the local time... or just let JavaScript work out the current time on the user's system.
As far as I know, there's no way of getting the time zone from JavaScript accurately. You can get the current offset from UTC, but that's not the same as the time zone itself. (Offsets change due to things like DST; knowing the current offset doesn't tell you when DST will kick in.)

Maybe machines have different time settings? If development and live servers are in different locations, that's quite possible. As other answers say, DateTime.Now gets server time, not client time.

Related

Timestamps with timezones

I am coding an App which users can post and it keeps a timestamp. I generate the timestamp in PHP using date('Y-m-d H:i:s'). Then when I display the posts the App calculates the time difference in c# to display in time ago such as 1 day ago or 1 hour ago and when you view post info it shows the exact date in the database.
The problem is when I create a post it displays as 8 hours ago since the PHP database isn't in the same timezone as the phone. I was thinking about calculating the time ago in the PHP but then in the post date, it would be off.
Also I can't do it off the devices time in case the user changes their settings then they can change their date back years and post which will create a bug.
Generally, the best practice is to generate timestamps on the server in UTC time, and then if necessary adjust them on the client to display in the user's local time.
You can use php's gmdate() function to get the current UTC time.

How to display time according timezone selection?

My Server has different time zone and I want to access my website from different timezone. So I want to display time in website according to system from which I will open website not want to set time according to server timezone.
Is there any way to solve my problem?
Please help me.
Thanks in Advance.
You can use javascript for same.
Add below code in your html (design) page
var currentdate = new Date()
This will give you current Date-time at end users machine.
var offset = currentdate .getTimezoneOffset()
This will give you current time zone offset with actual GMT time.
Using this value you can also identify the End-users time zone.

Is GPS timestamp from the GPS signal or the device?

My Windows Phone app uses location tracking, and I am stamping the positions I use, with the system date and time.
This has worked well, but now there was a situation with a user, who had his date/time setup horribly wrong on his phone, meaning that the timestamps on his positions were all wrong.
So, I was looking into the Timestamp property on the Geocoordinate instead, but my question is: Is that timestamp really from the GPS unit, or is it just the system (device) date/time of when the position was obtained?
The documentation on MSDN says it is:
The system time at which the location was determined.
If that is the case I really don't see much use for it, but has anyone tested or have experience with this?
I have always interpreted this as the Systemtime but your question made me curios. So I tested this by experiment, switching off the set-time automatically and changing the the time somewhat will indeed change the timestamp of the Geocoordinate. While I have not found a good source to collaborate this my experiment makes me believe that your assessment is correct.
A way to go around this problem for your app could be to get the time from an online time service such as timezonedb api ?

Time is not right in online version

On my website i show people the time when they open my page. So when they opened it at 4/29/2013 10:09 AM it wil show: 4/29/2013 10:09:14 AM
This all is working fine on my localversion.
But now i have my website online and the time is showing with 5 minutes delay.
I opened it at: 4/29/2013 10:10 AM it shows me: 29-4-2013 10:05:19
Who can help me with showing the right time online.
The time i want to show needs to be the local time.
The code:
<td>Capture Time:</td> // label
<td>#DateTime.Now.ToString()</td>
The time you are showing is server time ... and you are expecting it to be the same as local time.
If your server is in a different timezone than your client machine, the time shown will be different.
You may want to consider using javascript and do this on the client instead.
Or show the server time in a standard format like UTC and the client can try to determine the difference from local time.
DateTime.Now will show you server datetime. If your server & you are located in same timezone, then I think your local system time is different from server datetime.
Also, if your server is in different timezone then you should consider converting in your timezone before showing datetime on the UI.
Convert Time in Different TimeZones in ASP.NET
This will take server side date and time. If the server is in USA and if you are in India then you will get 12 hours difference. To solve this create a hidden input field and then wire a Javascript routine to the onsubmit event for the form. This routine would populate the hidden field with the time on the client machine.
The hidden field can used with ASP.NET by using the HTML control "HtmlInputHidden" class. You just give you input control a runat="server" attribute like any other server side control.
The server can then read out this time when the form posts back. You could even wrap this up in a server control if you need to do this in a number of places.
var now = new Date();
now.format("dd/M/yy h:mm tt");
Copy this date time to a hidden field which have runat="server" property and take that value from server.
Looks like it's displaying in a different format (dd-MM-yyyy while you want MM/dd/yyyy). Make sure your culture is set correctly in the web.config on your production server:
<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8"
culture="en-US" />
</system.web>
Also, try specifying the format in the ToString method:
DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt");
This will display the date in the format you want. The time, however, is dependent on the server's time and won't necessarily match the time on your local machine.

Is it possible to specify the time zone on Windows Azure?

I am going to migrating a system to Windows Azure. And it will used UTC time for all existing function. Is there any way to set the time zone globally? Or I need to change all the code which display the time? My application will mainly serve in a specified timezone.
I have try apply the culture and uiculture on web.config. And it does not work.
Thanks.
As per the Windows Azure Team Blog - all the timing calculations/display etc have been moved to UTC.. https://azure.microsoft.com/en-us/blog/moving-to-coordinated-universal-time-utc/
I feel you would need to change the code which display the time as well which saves the time as well..

Categories