'Easiest way to flip dates between US and Uk

I have a Blazor Server app, its users are based both the UK and US. Is there an easy way to flip the date to the appropriate MMM dd yy (for US) and dd MMM yy (for UK) users. I have various date fields dotted around in pages/components/grids in my app:

@(data?.End_Date?.ToString("dd MMM yy"))

If there a simple way I can put the date n an IF statement to check if the user browser culture is UK or US?

Or is there another way I should be doing this?

TIA



Solution 1:[1]

You can use the IFormatProvider overload for ToString.

This outputs the date in US format:

date.ToString(CultureInfo.GetCultureInfo("en-US").DateTimeFormat);

This outputs the date in GB format:

date.ToString(CultureInfo.GetCultureInfo("en-GB").DateTimeFormat);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Grizzlly