-1

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

OldProgrammer
  • 12,050
  • 4
  • 24
  • 45
  • you need to post a [mcve] – OldProgrammer Aug 27 '21 at 18:53
  • 4
    At least post the actual code you use to create the string! – stuartd Aug 27 '21 at 18:56
  • 2
    Does this answer your question? [Datetime acting weird on different computers](https://stackoverflow.com/questions/30313557/datetime-acting-weird-on-different-computers) – gunr2171 Aug 27 '21 at 19:01
  • Relevant: [System.Globalization.DateTimeFormatInfo.DateSeparator](https://learn.microsoft.com/en-us/dotnet/api/system.globalization.datetimeformatinfo.dateseparator?view=net-5.0#:~:text=The%20DateSeparator%20property%20defines%20the,string%20in%20a%20parsing%20operation.) – John Wu Aug 27 '21 at 19:12
  • 2
    If you need a fixed date format that is consitent on every machine in every language, you have to provide (at least) the format string, and (better also) the respective CultureInfo – derpirscher Aug 27 '21 at 19:18
  • 2
    @gunr2171 while the referenced question somehow addresses a localization issue of dates, I wouldn't recommend the accepted answer at all because it's absolutely discouraged to build SQL queries that way ... – derpirscher Aug 27 '21 at 19:22

1 Answers1

3

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")
Spencer Bench
  • 647
  • 4
  • 7