CompareValidator date format - c#

I have an ajax calendar control that I specified the date format to be yyyy-MM-dd. Is it possible to have the comparevalidator validate that type of date sepcifically and have it fail for everything else? right now it seems to only take dd-MM-yyyy.
Thanks.

I assume validators use the current CultureInfo object to determine the date format.
CultureInfo.DateTimeInfo.ShortDatePattern
Try changing the page culture(MSDN).
If you don't want to go that route you can always create a custom validator.

Set CultureInvariantValues="true"
from the docs:
Culture Invariant Values - When doing
conversion on a compare validator's
non strongly-typed properties
(CompareValidator.ValueToCompare,
RangeValidator.MaximumValue,
RangeValidator.MinimumValue) the
validator will use a culture neutral
format (Date: YYYY/MM/DD, Double &
Currency: US culture format) to do the
conversion when CultureInvariantValues
is true.
EDIT
Searching on the web, I found a similar question in another forum, see:
http://forums.asp.net/t/1229615.aspx, maybe the last answer (that was gave by Mohan.Raju) can solve your problem too.

Related

Convert Persian date to standard English date

I have a textbox with this mask: year/ month /day hour :min
The datetime format is Persian like 1392/12/11 12:43
I need to convert this string to standard English format so I used pesiancalender class.
As you can see the function todate() expects the values separately, I don't know how can I separate the string to this values! I mean I don't know how can I detect year and month and day and hour and min in string.
You can use either DateTime.TryParseExact method with providing the date format to it with culture info about persian date.
Edit: as I found out:
Currently, the PersianCalendar class is not an optional calendar for any culture supported by the CultureInfo class and consequently cannot be a default calendar.
So, you can't use the approach I've suggested. Some investigation led me to the this project for working with Persian date time and some hacks for the CultureInfo.
Such questions were already on SO, so I suggest to use their approach, and to write some helper class to solve your problem.

Move to Following Date Part on Data Entry in DateTimePicker

I have a user request that I'm trying to accommodate, the simplest way I can think to explain is to use illustrate with a picture:
Essentially the user is typing a ton of dates in. Instead of typing the
MM [Backslash (Or Right Arrow)]
DD [Backslash]
YYYY [Backslash] etc ...
They'd like to Key the value and be moved to the next Date Part Value. While this doesn't seem like much I can imagine it adds up to allot of additional keystrokes and takes the end user off the number pad. I'm currently using Telerik's Winform RadDateTimePicker with Custom Format:
MM/dd/yyyy hh:mm tt
but would not be opposed to changing to the Winforms DateTimePicker.
Can anyone suggest an elegant solution for this ? The only thing I can currently think of is catching the keystrokes when the control is in focus but I feel like this could be messy.
Thankyou
RadMaskedEditBox (and respectively RadDateTimeEditor, which uses RadMaskedEditBox internally), have build in functionality to select the next part, however, it seems it does not work correctly with this mask, so I reported it as an issue. Here is the link to it: issue link. You can subscribe for status change alerts in order to get notified once its done.
What you can look at is the free form date time parsing introduced recently. It allows you to type in "10162014" which will get parsed to "10\16\2014". More information no parsing dates is available here: Parsing Dates
And the last thing would be to work with KeyDown and manually move the selection. There is API for this on the provider. Here is how to access it:
(this.radDateTimePicker1.DateTimePickerElement.TextBoxElement.Provider as MaskDateTimeProvider).SelectNextEditableItem();
I hope some of this works for you.
This is not intended to be a link answer but I have to refer to the Telerik documentation to allow the OP to customize the control according to his needs.
My understanding is that your customer needs to type the date and time in a specific culture avoiding to type the separators.
You should use the maskedEditBox which already have a mask for your need MM/dd/yyyy hh:mm tt
Particularly you should set the pattern "s" which correspond to "Sortable date time pattern (based on ISO 8601) using local time"
Have a look at Telerik documentation here to set up your control as needed and the relevant validator.
This should solve your problem.
This is how it looks when empty:
and this si show it appears when filled in:
Using the relevant event (I have used radMaskedEditBox1_Leave) you can still use also a dateTimePicker taking the input from the radMasketTextBox:
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "dd/MM/yyyy HH:mm ss";
dateTimePicker1.Text = radMaskedEditBox1.Text;
Please check below URL fiddle URL for datetime mask:
1]. http://jsfiddle.net/jensbits/3yUes/light/
2]. http://jsfiddle.net/LkpPJ/5/

Datetime format inconsistencies in C# and jQuery

I have an issue, i have a user based datetime format setting.
Now, i set the input format in the Global.asax as shown below
void MvcApplication_PreRequestHandlerExecute(object sender, EventArgs e)
{
if (userSettings.Settings.ContainsKey(Constants.DateFormat))
{
CultureInfo cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
cultureInfo.DateTimeFormat.DateSeparator = "/";
cultureInfo.DateTimeFormat.FullDateTimePattern = userSettings.Setting.Attributes[Constants.DateFormat];
cultureInfo.DateTimeFormat.LongDatePattern = userSettings.Setting.Attributes[Constants.DateFormat];
cultureInfo.DateTimeFormat.ShortDatePattern = userSettings.Setting.Attributes[Constants.DateFormat];
}
}
But when i bind the date values from a UI textbox to a property, i get only null values. what am i missing in this process.
the date format that the user has given is 'dd/mm/yy'. after MVC model binding is done, the input of '01/11/12' is bound as 11th january 2012.
Kindly suggest the issue and where will the fix be working out.
EDIT
Is it possible only to have month as first in a dateformat string like mm/dd/yy. is it not possible to have dd/mm/yy as a format string.
something like this?
cultureInfo.DateTimeFormat.LongDatePattern = CultureInfo.CreateSpecificCulture("id-ID");
The example is taking culture from Indonesia, we use dd/MM/yyyy format.
Perhaps you should look into a formal date format that can then be localized. We chose to go with ISO 8601 (which is now the default for WebAPI). Then, you can be confident on the client that you are going to read the correct date from the serialized format. However, then you need to localize the time (if needed).
Scott Hanselman has a good write up of the issues.
And this datetime javascript library looks interesting.

DateTime Conversion with Different Cultures

I am wondering what is the best way to figure out how to convert a datetime?
Right now my computer is setup as dd/mm/yyyy yet I am taking date as mm/dd/yyy so when I try to do
DateTime.Convert();
DateTime.Parse();
DateTime.TryParse();
I either get nothing back or it crashes. Now I could tell it about the mm/dd/yyyy format and it probably would convert. However the problem is these dates are are coming from user uploaded files.
They could be in any format combination. I am trying to find ways to do it.
The problem I see is that I am looking at the dates in an uploaded file so I am not sure if looking say at the browser date format would help or not.
I am using asp.net mvc and I am trying to get a solution that handle date formats automatically so I don't have to ask the user about the date format they have (I figure the more things I have to ask them the less likely the user will continue on)
No, you can't figure out automatically what date-time format a user meant to use once the value is on the server. You need more information to parse it correctly (e.g. 1/2/3 can mean a lot of different dates depending on the culture).
Consider one of the following solutions:
Convert the entered date to a text representation in a standard format (i.e. ISO 8601 - 2012-02-09) using JavaScript on the client before you send it to the server. The code would look something like this: d.getUTCFullYear()+"-" + d.getUTCMonth() + "-" + d.getUTCDate().
Send the local culture information to the server along with date value to be converted and do the conversion on the server.
Force the user to enter the date in a specific format (e.g. Use 3 text boxes labeled "Month", "Day", and "Year" instead of one text box with free input).
chobo2 (I like the 'handle') :)
you can detect the locale culture and work on that at will. see the following SO Q/A for pointers:
Where is the system locale/culture set for .Net
the key is to NOT have to set anything in particular, but identify the locale and act accordingly.

current date format

Is there any way to find the current format of date in the time zone? I am retrieving date in the form of string from database and in case the current datetime format does not match, crash comes, "String was not recognized as valid datetime"
It sounds like what's important isn't the current format of the date as your code understand it, but as it gets it from the database. Why is it in the database as a string to start with? If at all possible you should make it an appropriate date/time related field in the database and make the driver do the conversion.
If that's not possible, you should perform the conversion in your code using a custom date/time format which matches what the server gives you, and in an appropriate culture (quite possibly the invariant one).
The DateTime.Parse method uses the format that is set on the executing thread. See the Thread.CurrentCulture to retrieve the CultureInfo that use used when parsing. The CultureInfo.DateTimeFormat returns the format you are looking for.
If you know the format, you should use the DateTime.ParseExact method to parse the input string with a known format.
Sound like you need to use the DateTime.TryParse-method:
http://msdn.microsoft.com/en-us/library/system.datetime.tryparse.aspx
If you don't know in which format the date is passed and .NET can't figure it out I think your out of luck. You could of cause try to see if you could figure out the format by yourself by using regex.

Categories

Resources