I am running a website in IIS on my local PC, I save a date as UTC and call .ToLocalTime() on it in the front end mvc website. If I run the website on my local PC it outputs the correct date and time adjusted for DST. If I run it in Azure in region UK West I get 1 hour behind. This hasn't been an issue until today i.e. the clocks changed on Saturday. Do I need to raise a ticket with Azure? Or am I doing it wrong?
Azure App server is using UTC time. If your site is not global just for UK. We can use the following code to get the UK local time.
DateTime utcNow = DateTime.UtcNow;
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time"); //UK time zone
DateTimeOffset time = TimeZoneInfo.ConvertTimeFromUtc(utcNow, tz);
lab_time.Text = time.DateTime.ToString(CultureInfo.InvariantCulture); //display the datetime with label.
If your website is to be global that you may need to use javascrpt to do that, more detail please refer to another SO Thread.
Time-zone depends on the the server location literally.
I had faced the same issue. I had my server in Singapore and it used to clash with my zone, India.
It would affect the time and date formats too.
So, Better not use the ToLocalTime() at the server side. just get the UTC time from the server and convert to whatever local time at the client side. Issue resolved!
Hope this helps. Thank you.
Related
The user profile page doesn't appear to allow the user to specify their timezone. How should we add this feature so the user can select and save their timezone and then have the web pages display the localised date?
Times are displaying in my browser according to the location of the server, so I edit a record at 7am local time, and it appears as if I edited it 4 hours in the future :)
Best solution for me is to allow the user to specify their timezone on their profile and then have this setting reflected in times displayed in the UI.
I'm set Clock.Provider to the UTC in file Startup.cs of ...Web.Host is OK
// using Abp.Timing;
public Startup(IHostingEnvironment env)
{
// Set Clock.Provider as UTC:
Clock.Provider = ClockProviders.Utc;
//_appConfiguration = env.GetAppConfiguration();
}
If Angular not sent date UTC you can using:
yourDate = abp.timing.convertToUserTimezone(yourDate); // to convert yourDate to date UTC.
You need to set the Clock.Provider to the UTC clock on the server. The documentation tells you that you need to set it but doesn't really tell you where. You just need to set this during the startup process. Here is a link to the docs that touch on it briefly.
Timing Documentation
Hope that helps.
I wrote a .NET program that creates live events in Youtube, but I cannot set the correct starting time and some settings like Keywords, and Region. This is what I do, when I create the Broadcast I put the starting Date and Time this way:
oLiveBroadcast.Snippet.ScheduledStartTime = String.Format(CDate(txtScheduledStart.Text), "yyyy-MM-ddTHH:mm:sszzzz")
...but the GMT is always referred to US (GMT -8). I think that the problem is in the receiving field .Snippet.ScheduledStartTime which has a dateTime format not including GMT. What can I do?
I also didn't find how and whare to set Region (in my case Itali IT-it) and searching Keywords
Thanks
In my project i saving the selected datetime(in UTC) from my datepicker.It saves the utc datetime only while application running in local.After deploying to azure it saves the selected datetime from datepicker irrespective of utc.
Update :
string selecteddate = "12/04/2016 05:30:15";
DateTime dt = Convert.ToDateTime(selecteddate).ToUniversalTime();
this code saves utc datetime in db in localhost,But it not working in azure
The DateTime instance you create with Convert.ToDateTime(selecteddate) will have DateTimeKind.Unspecified. Because of this, when you then call ToUniversalTime, it is making the assumption that the source value is in the local time zone of the computer where the code is running.
On Azure, that time zone is already UTC, so while the .Kind will change to DateTimeKind.Utc, the value will stay the same.
If you intended it to be converted from a particular time zone, then use TimeZoneInfo.ConvertTimeToUtc instead, passing a specific TimeZoneInfo value for the time zone you care about.
In general, don't rely on the local time zone setting in a web app, as the time zone could be different depending on where you deploy. Avoid ToUniversalTime and ToLocalTime, as well as DateTime.Now and other functions that depend on the local machine's time zone setting.
Quick environment overview:
I have an ASP.NET MVC/AngularJS application that is ultimately being hosted as an Azure Webrole. The data is stored in MongoDB hosted in the same Azure data center (via MongoLab). Development environment and staging environment (Azure) point to same MongoDB database and thus see the exact same data.
Issue:
Individual users of the application are stored with their preferred timezone as a string (TimezoneID). All dates in the app are stored as UTC times. Conversion to and from UTC times to a users specific timezone is done on the server via an extension method which ultimately calls:
return TimeZoneInfo.ConvertTimeFromUtc(UtcDate, _lookup[TimeZoneID]);
Nowhere in the entire app is there a DateTime.Now or any other date conversion I would expect to use the server time settings.
In a particular area of the application a date is being stored in Mongo (UTC). I can confirm that the date is correctly stored as UTC (based on the offset from my local time). When I retrieve that date from the data store and convert it back to my local time (Central Standard Time) I step over the above line of code and the conversion works fine and the local time is set. The result is sent to the client's browser as part of a Json result of an API call and displayed to the user correctly. During transport the MVC Json serializer parses the datetime into a format similar to:
EventDate: "/Date(1399407153971)/"
I parse this in Javascript and display accordingly and all is good.
When I deploy the exact same code to Azure (which points to the exact same data store in Mongo) the result that comes down via the API call appears to have applied the UTC transformation twice (meaning the regular 6 hour UTC offset is applied twice and the displayed date is 12 hours earlier than UTC...6 hours off of what I want).
I've updated the stored time in Mongo by a minute or two to make sure both sides update and they do (verifying the same data). I've modified the date/time settings of my local machine to see if it has any effect on my local results and it does not. I've changed the timezone for the selected user in the app and both my local result AND the Azure returned result adjust by the UTC offset difference between Central Standard Time and the new time zone (meaning if I switch to Mountain Time the result of both the local result and the Azure result adjust by 1 hour...but are still 6 hours off). I've confirmed via Chrome Dev Tools that the date in question that is retrieved at the client (via Json) is different based on whether the site hit was the remove (Azure) environment or local environment (meaning it isn't being modified incorrectly anywhere on the client after-the-fact)
Here is the most telling thing:
If I convert the date to a string prior to returning it to the client the string sent down appears correct in the Json response. If I leave it as a .NET DateTime type (as a property of a complex object) the Json serializer appears to translate it again but ONLY when hosted in Azure. This has pointed me to the issue being somehow related to the DateTimeKind of the date I'm returning. Although, at this point, I'm too lost in the weeks to see why the difference between my local environment and the Azure host environment in serialization and why one things a time is correct and the other converts it twice. If the DateKindTime was wrong wouldn't both environments perform the same conversion?
Thanks for reading through this and I'd appreciate any ideas to address anyone has.
It's difficult to say for sure without seeing more code, but could it be that the problem is with conversion on the way in, rather than on the way out? You said that you're using TimeZoneInfo.ConvertTimeFromUtc for the output. Are you also using TimeZoneInfo.ConvertTimeToUtc on the other side? If so, that's likely the source of picking up your local time zone.
You might also somewhere have a call to DateTime.ToUniversalTime, which again - uses the local time zone.
Another area which might be the culprit - when you retrieve your value from Mongo, check the Kind of the values coming back. If they're Unspecified, then when they are serialized to JSON, they will be treated as if they were Local. basically, there's a ToUniversalTime call going on under the hood. So you may need to explicitly call DateTime.SpecifyKind to set the value to Utc before it goes out the door.
With specific regards to Azure, it follows best practices of setting the server time zone to UTC. You could try that on your own machine and see if you get similar results.
Of course, you really don't want the server's time zone to influence the result at all.
You may want to consider creating a new project with a minimal, complete, and verifiable example. That will help you verify your assumptions, and will likely track down the source of the problem. If it still fails, well then you'll be in much better shape for asking for help.
That was a long post :)
If I were you I would transfer all dateTimes in UTC format. The server basically must work with UTC time as far as I'm concerned. Transformation to client time must be applied at clients side. You can use momentjs library that proved to be very fine library for working with dates and times.
On client side do something like this
var date = {
utc: '/Date(1399407153971)/', //time that you have recieved
offset: 240
}
var localTime = moment.utc(date.utc).zone(date.offset).format('DD/MM/YYYY hh:mm');
I hope this helps
UPD
As Matt Johnson pointed out you can make it even shorter for current time zone.
var localTime = moment('/Date(1399407153971)/').format('DD/MM/YYYY hh:mm');
I have a weather website and I'm driving me crazy to figure out how I can have two clocks on my page and they will be updating in real time. One with the 'Europe/Lisbon' timezone and the and the other with the 'UTC' timezone
Now I have this code lines:
<strong>Elvas:</strong>
<?php
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('Europe/Lisbon');
// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('H:i');
?>
   
<strong>UTC:</strong>
<?php
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('H:i');
?>
But of course with no automatic updates as I would like.
Thanks in advance for any posible help you could give to me.
PHP is a server-side technology. If you want to update your page in real-time, you need to stick with JavaScript.
By default, JavaScript doesn't know anything about time zones, other than the time zone of the computer it is running on. In order to display the time in some other time zone, you will need a time zone database for JavaScript. I list several of them here.
For example, you can use moment.js with the moment-timezone add-on. Getting the current time in a specific time zone is like this:
moment().tz('Europe/Lisbon').format('YYYY-MM-DD HH:mm:ss')
To update it periodically, you can use window.setInterval.
If you are looking for a very easy solution already coded for you, consider using the "Free Clocks for Your Website" offered at timeanddate.com. They are very reliable and customizable for different time zones.