I am sending ajax request and parameters in url one of which is date. The format in url looks like dd.mm.yyyy. When goes in controller DateTime parameter reads it as mm/DD/yyyy. How can I change this behaviour and tell mvc to read it as dd/MM/yyyy ?
Example:
URL: 12.01.2017
MVC: 01.12.2017
That depends on the configuration that you have on your browser for the culture, unless you POST the data in the format you want.
On IE,
Click Internet Options.
Click Languages, under the General tab.
Click Add...
Select the language you would like to add.
Click OK.
Then move the language that you want up to have the priority over the others, the Culture will come from there and the date format will change.
You have to set the format attribute in the constructor of your datetimepicker assuming you are using jquery.
For example:
$('.datepicker').datepicker({ "format": 'dd/mm/yyyy' });
Related
I'm probably blind to something right in front of me.
But In my MVC application, I have an object/model that has a DateTime property.
I do an AJAX GET to retrieve the model. The datetime when received looks something like Date(142342323).
I want to convert this date to the locale setting of the user. In moment.js I don't see a way to set it to local.
I thought about getting the currentculture in MVC and passing that as a value (and storing it as an hidden field on the page ) and then using that for the javascript date format...but there seems to be a discrepancy between c# formats and javascript formats.
Ideas anyone?
I mainly have 1 format for Europe and 1 for US (dd/mm/yyyy and mm/dd/yyyy).
How do I convert an anonymous date format to my own system culture info?
I am using a jquery calendar .datepicker(), but when I'm trying to get the value of that text box in the c# datetime variable, it shows invalid date time string. I have tried datetime.tryparse ,datetime.parse,convert.todatetime() and many others.
Now I am trying to get the date in jquery like var a = date.now();
But I am getting the following error: date.prototype //invalid date
I don't know why this is happening.
Reading your comments (" i am also getting error in prototype= invalid date and when i am trying to using this var a to set the date in another calendar"), I think you're trying to set calendars within jquery, possibly within an asp.net page.
If this is the case, you can set the date format when you create the datepicker :
$( ".calendar" ).datepicker({ dateFormat: "mm-dd-yyyy" }); // 10-25-2013
Format it as you need. For a list of date formatting options, see http://api.jqueryui.com/datepicker/#utility-formatDate.
If this doesn't help, it would be useful to know :
* what is emitting the date
* what is consuming the date (jquery/ C#)
* the actual code that is generating errors, if any.
After reading your responses, I think you should try specifying the format of your datepicker explicitly.
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.
I have a object that im serializing to a JSON object using JSON.Net. This object is then being consumed by a JSON Store which is attached to a ExtJs GridPanel.
All fields except date fields render fine , the only way i can render date fields is if i use text columns. But then i get the following /Date(1293746400000+0200)/ rendered as text which is useless.
I know i need to convert that somehow to a proper date object but i have not idea how atm.
Let me know if you need more info.
"M$" in the preceeding example is misleading, its "MS". Here is a clear example that works in extjs 4. The 'LastFellOffCliff' field is set to be a date with incoming format in the Microsoft JSON date style: "/Date(...)/"
Ext.define('ACME.model.CoyoteModel', {
extend: 'Ext.data.Model',
fields: [
{ name: 'CoyoteID', type: 'int' },
'Nickname',
{ name: 'LastFellOffCliff', type: 'date', dateFormat: 'MS' },
'Notes'
]
});
JSON.Net has various date/time converters to help you deal with this. See this blog post for some details. So you could use the JavaScriptDateTimeConverter for example, then eval the result into a JS date object. I can't recall off the top of my head how an Ext store will deal with that, but maybe it will point you in the right direction.
Have a look at here.
In most cases you can pass your own formatted string (i use "yyyy-MM-dd" in my projects as i don't need time) and re-use it on the other side (format it as a valid date constructor).
In the field(s) that is/are for the date in your JsonStore, you can set the field's type to 'date' and its dateFormat to 'M$'. Works like a champ.
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.