DateTimeOffset.ToString() formatted output not consistent across enviros - c#

I am formatting a date for integration with Moneris' recurring billing. The request requires a start billing date formatted as "yyyy/MM/dd". Simple task and "it works in Dev".
When I publish the API code to our staging environment the submissions fails (with a very help-less error from Moneris, "System or data problem. Please try again."). I have determined that it is in fact the date format of the start date at issue.
In Dev, the output is as instructed above, however, in staging the output comes out formatted as "yyyy-MM-dd". This breaks the submission. I went so far as to hardcode a value with the correct format and it succeeded. So we know that the outputted format is the difference at issue.
I have not run into this issue with any other instance of ToString("blah") on a DateTime. Is it to do with a DateTimeOffset? Why would the formatting waiver from what was instructed?
The API is a C# Asp.NetCore 3 solution.
Development Enviro: Windows 10 Pro
Staging Environment: Windows Server 2019 Standard

In custom DateTime format strings, unescaped / represent the date separator, which is culture-specific. If the machine running your program is configured with en-US culture, then the separator is / as you'd expect. But ar-DZ uses -, tr-TR uses ., etc. (See here for more info.)
To ensure that the string is always formatted with / characters, you need to escape them:
date.ToString(#"yyyy\/MM\/dd")
or
date.ToString("yyyy'/'MM'/'dd")

Related

Why would GetCultureInfo() format differently on different servers?

I'm working with two servers (Server 2012 Std) and (Server 2016).
On 2012 when I run the command:
[System.DateTime]::UtcNow.ToString([System.Globalization.CultureInfo]::GetCultureInfo("en-NZ"))
I get back the result:
13/03/2018 12:49:55 a.m.
When I run the same command as above on my Server 2016 I get back:
13/03/2018 12:48:42 AM
They key part is the AM/PM formatting. I'm trying to understand why these are returning different results and how to get the server 2016 output to be formatted the same as the first. This is due to an application requirement and I do not have access to change the application to format the string so I have to resolve this at the OS level somehow.
Take a look at Output of times (AM/PM) changed in Windows 10 when using DateTime.ToString("tt") for some discussion of this.
It doesn't appear that you will be able to fix this without changing application code.
I found the same problem and I was able to get around the issue by using the following code. Not sure if that's what your looking for.
[System.DateTime]::UtcNow.ToString("dd/MM/yyyy hh:mm:ss tt")

Default locale settings formatting a datetime instance

I need to use string.Format("{0}", DateTimeInstance) in a server application running on iis. I was sure that the format applied was the one of the server but i find out that the date is formatted as dd/MM/yyyy... some times and MM/dd/yyyy other times. I am guessing this depends by the client (the browser connected) settings. Is this possible? If yes, can I override this behaviour and force the server settings, without specifing a format?

Why does date format changes to month first in date time object?

In a web application, I am passing datetime without formatting it in a XML response, just item.Date (DateTime datatype) is put in code.
No formatting is done. When I run the local server it returns the date in MM/DD/YYYY format and in live environment DD/MM/YYYY. Why this change is happening?
I checked on database collation and OS settings. Live environment had English (Singapore) in regional settings. However after changing local servers to English (Singapore) still live environment DateTime format is not being produced locally.
Collation:
In Live Environment - Latin1_General_CI_AI
In Local Servers - SQL_Latin1_General_CP1_CI_AS
What could be the reason? how to resolve this rather than doing formatting in XML?
Update
As suspected, Issue is with collation, regenerated the issue locally after creating DB with collation in live environment.
Windows stores Date/Time settings per user. If IIS is running as LocalSystem, then you'll need to change the settings of the Default user:
In regedit, go here and set the appropriate values up for the locale and format that you want.
HKEY_USERS\.DEFAULT\Control Panel\International
That said, handling dates and times types under the assumption of any given format is A Really Bad Idea.
If you need date/time in a specific format for display, interop or whatever you should format the date time appropriately with one of the available string formats.

How to resolve Date format change Issue after deployment in asp.net?

When I'm using DateTime in dd-MM-yyyy format.
When I debug my code at localhost its works fine.
But After deploying my ASP.NET web project on IIS server DateTime changes to mm-dd-yyyy format automatically.I'm facing many issues because of this problem.
I'm not able to find any solution, please let me know how can I solve this.
How can I get rid of this issue.?
1) Change the datetime format of your server from:
Control Panel -> Regional and Language Options -> Advanced
2) Open IIS and follow below steps: (For IIS7)
Click on you Website
Select .NET GLOBALIZATION option
From Culture tab, select required Culture and UI Culture.
Finally iisreset.
Your IIS probably has another Localization selected, than on your development machine.
Printing should be pretty simple if you specify the format: yourDate.ToString("dd.MM.yyyy");
Parsing a date has been a problem for me in the past. You can change the server settings or specify a CultureInfo directly in the code, like this:
DateTime.ParseExact(myDateString, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None);

Entity Framework - SQL Server 2005 - IIS Server datetime issue

I'm working with MVC3 and Entity Framework. In my application I need to call a stored procedure in SQL Server 2005 via EF to search for some data according to datetime parameters passed.
Everything seems to be working fine in local environment. But after hosting it into IIS I am getting an exception while trying to search from date 13-08-2012 (13 is taking as month in SQL I guess)
Error says
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and
12/31/9999 11:59:59 PM
I understood error is because of the difference between date time formats between System.Datetime and SqlDatetime.
But I didn't understand why it is working without any issues in my local environment which uses same SQL Server but getting this error after hosting in IIS server.
Is there any workaround for this issue?
My issue is resolved now. Issue was with the culture settings in IIS.
I've added these line to my applications web.config and it is working fine now.
<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB"/>
For more information check out this Issue with culture settings in IIS
IT depends on the culture of the server, you can format your culture with invariant format, on your date 13-08-2012, it consider 13 as month.
//Here an example of formatting with invariant culture
CultureInfo yourCulture = CultureInfo.InvariantCulture;
yourValue.ToString("yourFormat",yourCulture));
Verify that the date time formatting. Also, DateTime doesn't have the same range as SqlDateTime. You'll run into trouble if you receive an empty value.

Categories

Resources