Posting localized Date formats in ASP.net core with bootstrap datepicker - asp.net-core

We currently use bootstrap datepicker in our asp.net core application to select dates that essentially are localized. So I've added all the locale files for the specific languages and these all work fine until I issue a form submit.
For instance say the locale is es for spanish with a date like 27 OKT 2021 I get an error RE:
The date is not a valid date
How can I convert this back to say en-GB on post so that my model stays valid ?
see the settings for my datepicker in the cshtml view below:
$(".datepicker").datepicker({
//format: "dd M yyyy",
language: "#CultureInfo.CurrentUICulture.ToString()",
weekStart: 1,
maxViewMode: 2,
todayHighlight: true,
startDate: startDate,
daysOfWeekDisabled: [],
showOnfocus: true,
autoClose: true,
orientation: "bottom"
});
I also have a date format set in the cshtml view like so :
asp-format="{0:dd MMM yyyy}

A best practice for working with dates and times is to use ISO 8601 formats, which are standard and universal. This way you avoid localization problems and only store unambiguous dates and times.
For dates, the format is:
YYYY-MM-DD
In JavaScript you can convert a Date object to ISO format using toISOString(). See docs.

Related

how can i change the date format in asp boiler plate 3.1

Want to change my input type date format dddd, dd-MM-yyyy.
Any update or guidance?
try input tostring("yyyy-MM-dd") but it is default format.
Actually, I am using an asp boilerplate platform. I found that the platform only follows this date format 'yyyy-MM-dd. And this is working.

How to get UTC TimeZone DisplayName .NET 4.0?

I set a winforms combobox with the time zones DisplayName:
var zoneList = TimeZoneInfo
.GetSystemTimeZones()
.Where(z => z.BaseUtcOffset.Minutes == 0)
.Select(z => z.DisplayName);
It shows a list like this:
...
(UTC-01:00) Azores
(UTC-01:00) Cabo Verde Is.
(UTC) Casablanca
(UTC) Co-ordinated Universal Time
(UTC) Dublin, Edinburgh, Lisbon, London
(UTC) Monrovia, Reykjavik
(UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna
...
I want to sent the default item to (UTC) Co-ordinated Universal Time. However, TimeZoneInfo.Utc.DisplayName returns UTC. It is unusual because the upper enumeration shows different DisplayName.
TimeZoneInfo.FindSystemTimeZoneById("UTC") also returns "UTC".
TimeZoneInfo.FindSystemTimeZoneById("(UTC) Co-ordinated Universal Time") and TimeZoneInfo.FindSystemTimeZoneById("Co-ordinated Universal Time") do not work.
How to get the localized string that corresponds to shown in the enumeration (UTC) Co-ordinated Universal Time?
The only solution I can figure out is to store the string "(UTC) Co-ordinated Universal Time", but I'm afraid the upper list may change in different localization.
A few things:
The DisplayName is not the ID. Only the values returned by the Id property are suitable for use with the FindSystemTimeZoneById method.
For your dropdown list, you should show the DisplayName, but you should store only the Id.
The DisplayName, StandardName and DaylightName properties are localized by the operating system, not by the globalization features of the .NET Framework.
The IDs will always be in English. They will never change for localization.
You should never show the IDs to the end-user or interpret them in any particular way. There are many different conflicting conventions used, and some that are just made up. See the section on Windows time zones in the timezone tag wiki for some examples.
"Coordinated" is a single non-hyphenated word. These are correct in Windows, so I think you must have added the hyphen yourself.
Recognize that the offsets shown in the display names are just the base offsets. They do not indicate whether or not the time zone adjusts for daylight saving time or not. Nor do the offsets in this list change when daylight saving time is in effect.
You are correct that TimeZoneInfo.Utc.DisplayName == "UTC", and so does TimeZoneInfo.FindSystemTimeZoneById("UTC").DisplayName. However, the UTC entry returned by TimeZoneInfo.GetSystemTimeZones() has the full form of the display name: "(UTC) Coordinated Universal Time". Therefore, if you're displaying a dropdown list by enumerating the results from TimeZoneInfo.GetSystemTimeZones(), then you can just use the results as-is.
But if you want to get that display name directly, you will either have to do this:
string s = TimeZoneInfo.GetSystemTimeZones().First(x => x.Id == "UTC").DisplayName;
Or, this:
string key = #"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\UTC";
string s = Registry.GetValue(key, "Display", null) as string;
Again, remember that all localization of time zone display names is done by the operating system. Therefore, this approach works fine for Windows desktop applications, but doesn't make a whole lot of sense for web apps. Also, if your application supports multiple languages internally, you won't be able to rely on .NET Globalization for any time zone display names. In that scenario, you will need to provide your own source of display names, perhaps in a .resx file, or you can look into using my TimeZoneNames library.
Hmm, I found the answer by printing the time zone's Id.
The desired string is printed with:
TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time"); // Wrong
Edit:
As Matt Johnson noted, the above code shows the London time zone.
Here is another try:
TimeZoneInfo.GetSystemTimeZones().First(t=>t.Id=="UTC").DisplayName

jquery DateTimePicker validation for dd/mm/yy is not working correctly

I'm new in jquery. In my MVC project most of the validations are done in jquery. All are working fine expect this date field. Let me describe my problem.
I have a date textbox, which is attached to datetimepicker. My code is below.
$("#DateTimeTextBox").datetimepicker({
dateFormat: 'dd/mm/yy',
timeFormat: 'hh:mm'
});
For validating this field i'm using this code
'DateTimeTextBox': {
required: true,
date: true
},
Here if we give the date format is 'mm/dd/yy' for datetimepicker the validation is showing correct. if we give the date format like this 'dd/mm/yy', always show the validation error message "Please enter a valid date". But I'm giving the correct date like "25/11/2012".
Is there anywhere need to set the date format for validation.
Thanks
Bobbin Paulose
The additional-methods.js file is bundled with jquery validate and has a bunch of useful methods including a method 'dateITA' that you can use to validate a date in the format dd/mm/yyy. Download the latest query validate code here.
https://github.com/jzaefferer/jquery-validation
Add a reference to additional-methods.js in your page, and use the rule 'dateITA' instead of 'date'.

Date format in HTML cfgrid or SQL date format

I am trying to format dates in an HTML cfgrid. I cannot seem to make it work in CF when using HTML as the grid type. I have also tried doing it in MSSQL by using - CONVERT(VARCHAR(10), startDate, 101) AS startDate.
When I do that it shows up right in the grid but the grid will not sort on the date properly.
I understand why converting it to varchar screws up the sort but I cannot seem to make this work in either CF or SQL.
Anyone know of a way to make it show up in the grid in a mm/dd/yyyy format and also sort on the date properly?
Ability to use the mask attribute in html grids was added in CF9. To get it to work on dates you also have to specify type=date
<cfgridcolumn mask="m/d/Y" type="date" ... >
If you are using an html cfgrid, you need to use the formats found in the Ext JS Date class. NOT the date format for Flash.
Here is a link to the Ext JS Date class
<cfgridcolumn ... mask="mm/dd/yy">
source

dijit.form.DateTextBox setting values

I have following text field with dojo dojo Date text type.
dojoType="dijit.form.DateTextBox"
required="true"/>
I use the following code to set the dijit.form.DateTextBox value
dijit.byId('dtinv').attr('value',new
Date(tList[i].getAttribute("dtinv")))
;
tList[i].getAttribute("dtinv") has value 2010-04-02
When the value is set, I see 4/1/2010. Always the date in dojo date box shows one day less.
Is there something I should take care?
Dojo and Dijit process Dates in local time, not GMT, which itself may have been a poor design choice, but given this, if you use dojo.date.stamp.fromISOString() or pass the string "2010-04-02" as the value attribute in your HTML you will get April 2 at midnight in your local time. Pass that to the Dijit and it will be happy. Using the new ES5 Date constructor in Javascript will have different results:
(I'm in the Eastern timezone)
>>> new Date("2010-04-02")
Thu Apr 01 2010 20:00:00 GMT-0400 (EST) {}
that defines a date in GMT. However, the following object would work as expected with Dijit:
>>> new Date(2010, 3, 2)
Fri Apr 02 2010 00:00:00 GMT-0400 (EST) {}
Also, Javascript's new Date constructor is poorly defined when it comes to taking Strings. I'm not sure if the result you're getting is consistent across all browsers. You are advised to use dojo.date.stamp.fromISOString("2010-04-02") to get the corresponding Date object, or ES5 ISO date methods, where available in newer browsers.