Something wrong in the datetime format - c#

When i compile the application in my laptop it runs fine but when i run the same application in the server something is wrong with the date format. But when i checked the system date time format wit my laptop and server it the same format. Can anyone tell me what is wrong.

You also have to check the default culture set in the laptop and the server. Are they running in the same culture? Different culture settings will have different default time formats.

Is your application running on the server under a different user account? Regional settings are per-user so date and time formats will depend on the user that the application is running under. You can log in as the application user and check the Regional settings to determine if that's the issue.
This is really common in ASP.NET where the developer builds the app on their workstation and upon deployment to QA or production they find that Regional settings differ because the App Pool is using a service identity.

Is the system clock running UTC, and the OS changing it to local time on the server. This is a pretty common configuration, but will break programs that bypass the OS's functions to retrieve the date/time.

Related

DateTime.Now returning different times on local and azure app service

I am from west India. DateTime.Now returning current time when debugging from Visual Studio. But when I hosted it on an Azure App Service, it is returning 6 hours earlier time. The App Service is hosted in West India. I added App Setting for the App Service WEBSITE_TIME_ZONE to Indian Standard Time and restarted the App Service but still the same issue.
Indian Standard Time is not supported as App Service's timezone name, check supported time zone names here (Check exact spelling of the tz name in left-side column).
Best practise is to use UTC time in server context and convert it to local time in UI.
Azure server times runs on UTC by default. This way you could calculate the difference from the user time. There is also the option to use DateTime.UtcNow()

Does .NET DateTime access host machine time or virtual machine time?

I ran into an issue with an application running in a remote location through a Citrix server. When the application was run in a remote location it logged an event to the database with the time from the remote location even through the user was accessing the application through Citrix.
Did .NET's DateTime.Now somehow "escape" the Citrix container and find the time of the remote machine or is there some other explanation?
Here is the current setup of the application.
The application logs events to a database in the US
The remote user is located in Europe and access the application through Citrix which simulates running the application in the US
The Citrix server machine time is set to the US time
P.S. The application has been updated to query the time from the database when saving in the future.
A few things:
Citrix's "time zone redirection" feature is probably the explanation for what you've observed. The details are specific to what particular Citrix product you are using (which you didn't supply). You may be able to disable that feature. Google it, or ask on ServerFault.
Really, you should avoid using DateTime.Now in server-based applications. It should be irrelevant what the server's time zone setting is. Use DateTime.UtcNow, DateTimeOffset.UtcNow, or DateTimeOffset.Now, along with the TimeZoneInfo class, as appropriate. Alternatively, use Noda Time.
To answer the question in the title, you can see the source code for DateTime.Now here. It works as follows:
Call DateTime.UtcNow, which itself calls the GetSystemTimeAsFileTime Win32 API. This is the system clock. It is in terms of UTC - not any specific time zone. If you're running in a VM, typically this system clock is synchronized with time from the host.
Get data about the local machine's time zone. It uses a truncated version of the complete time zone (for performance reasons), and it caches the data for future reuse.
Note that if Citrix is redirecting the time zone, it will affect this step.
Using the data retrieved, calculate the offset from UTC - for the specific UTC point retrieved in step 1.
Apply the offset to get local time, truncating if MinTicks or MaxTicks are exceeded in the conversion.
Mark the value calculated in step 4 with DateTimeKind.Local, and return it in a DateTime struct.

Timezone related issue with asp.net application

I have one existing ASP.NET application. This application built in .net framework 2.0
We used DateTime.Now at many places to store current datetime in application. Its for different purposes including logging.
Currently application hosted on server which of USA. When I access application from my machine, it returns current datetime of server. Is there any way, I can get my local machine.
I researched and got that I need to use TimeZone class to get current datetime of my local machine. But in that case, I need to change wherever I used Datetime.Now and get time from timezone.
Is there any way, I can set globally and when I use datetime.now, it returns local machine time?
Please help.
No, you can't. There is no way to set TimeZone globally.
Best options:
Convert DateTime.Now to DateTime.UtcNow and do the appropriate
calculations (what if you have to move the server to another zone of
US?)
Use DateTimeOffset insted of DateTime which use the TimeZone.
In any case, you have to handle the date time conversion to the local time zone in client side of you app, not in the server side.
You could also possibly set the timezone on the server itself to the one you need.

How to access my computer's locale settings in the wpf app

I have a WPF(Windows) App for which I need to get the locale of the user or the system where application is running. I have changed the locale of my system to French as shown in the image and in the code I call "CultureInfo.CurrentCulture.Name", which is returning en-US always even though I change the locale of the system. Am I missing something here? Or is this not the right API to be used?
To solve your problem you need to use Windows Sensor API
https://code.msdn.microsoft.com/windowsapps/Windows-7-Geolocation-API-25585fac

Process.Start specify culture

In one webapplication i have a IHttpHandler that when invoked starts an regular application that connects to a firebird database and amongst other things parses some dates from the database.
When run from the desktop the application does what is should without any problem. But when it is started from the httpHandler is ends up with the wrong culture and fails to parse the dates.
Ive set the culture i both web.config and CurrentThread.CurrentCulture but it still ends up with the wrong one.
All of this is run from an English Windows Server 2003 with the culture set to sv-SE.
So is there a way to start an application with System.Diganotics.Process.Start with a specific culture?
If there aren't which culture does the newly started process use?
I don't think there is a way to specify the culture. The CultureInfo class is specific to .NET, and as far as I know there is no app-specific notion of a culture in windows. There is no way to specify a culture using the Process and ProcessStartInfo classes.
However, I believe the culture is associated with the current user, so what may be happening, is that your Web app is running within a different user's context, so when started from the web app, the other app gets its' users' culture settings. Therefore, you could change the culture (regional settings) for the user that is used to run the web app.
On the other hand, if its' a .NET app you're running, you could just hardcode the culture into it.

Categories