How to display time according timezone selection? - c#

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.

Related

Angular-Gantt is not responding when changing timezone to different timezone other than Default UTC (Etc/UTC)

I am using Angularjs v1.7.5 and Angular-Gantt v2.0.0
In addition, I am using ASP.Net Boilerplate v3.2.4 as a backend. multiple tenancy is disabled.
When setting timezone in Setting as (e.g: UTC+02:00 Damascus) or any other timezone other than Default UTC (Etc/UTC) the page is not responding and browser is freezing.
I've already tries using MomentJs instead of default javascript date and it's not working.
so my question here is whether anyone has experienced this problem and what is the solution for it.
Workaround:
Disabling multiple timezone support in Asp.Net Boilterplate by setting local timezone
in Global.asax as next :
Clock.Provider = ClockProviders.Local;
has solved the problem.
But I'm looking for a better way to solve the problem while keeping multiple timezone support.
Thank you guys in advanced.

Get Timezone based on Selected place

In my MVC application I am selecting the company & doing my operations based on that.
as you can see in the Image below::
Here the problem is with the Timezone based on the company I have selected. Because I have to Insert the Datetime for the current Timezone.
We know in C# there are Inbuild properties like ::
DateTime.Now
DateTime.UtcNow
TimeZone.CurrentTimeZone
I just want the time based on companies selected from the above Dropdown in C#.
Please Help me on this,
Thanks in Advance.
You can get it by using Date.getTimezoneOffset(). This links might helps you.
http://aspdotnet-naresh.blogspot.in/2013/05/aspnet-how-to-convert-to-different-time.html
get client machine timezone in asp.net mvc
http://www.w3schools.com/jsref/jsref_getTimezoneOffset.asp
Get the default timezone for a country (via CultureInfo)
To start, I recommend http://nodatime.org, because they have some mapping functions from the more common Olson-format to the windows time zone format. Read more about this: Olson Time Zone Id to Windows Standard Format using Noda Time
To solve your problem, there are multiple options:
Save the name of the timezon in Olson format in your database.
Save the geolocation in your database. You can use the shape file with timezones to select the right timezone based on geolocations. http://efele.net/maps/tz/world/. There is also library to read this file: http://www.easygisdotnet.com/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..

Datetime.Now incorrectly determining my location

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.

Categories