The sole purpose of datePattern is to format the way the user types the date into the DateTextBox.
No matter what format the user types the date in, internally Dojo works in the ISO date formatby design. This also makes it easier on you, the programmer.
If you're looking into converting ISO into another proprietary format, there's a module for that.
require(['dojo/date/locale'], function(locale) {
var date = locale.parse('2016-01-28', {
datePattern: 'yyyy-MM-dd',
selector: 'date'
});
var formattedDate = locale.format(date, {
datePattern: 'MM-dd-yyyy',
selector: 'date'
});
console.log(formattedDate);
});
The probleme is in the dojo.formToJson
it return the default date format
whatever you specify the format in the dijit/form/DateTextBox
input.
So , I suggest to format the date inside the generated jsonForm ,
here is a solution :
First import the required js ,
require(["dojo/date/locale","dojo/json"],
function(locale,JSON){
......
})
"dojo/date/locale"
used here to change date pattern
"dojo/json"
used to Parse o json Object or reverse (Object to String)
then declare the formatJsonFormDates
function
( params are explained in the code it return a new jsonForm with formatted date)
helps you to convert all date at ones if there is many dates in the Form, by
passing the name
attribute of the input date in an Array parameter
var formatJsonFormDates = function(jsonForm,form,dateFieldsNames,datepattern){
if(!fieldsNames.length && fieldsNames.length < 1 ) return jsonForm;
jsonFormObject = JSON.parse(jsonForm);
for(var i = 0; i<fieldsNames.length ;i++){
if(form.getValues()[fieldsNames[i]] instanceof Date) {
newDate = locale.format(form.getValues()[fieldsNames[i]],{datePattern: datepattern, selector: "date"});
jsonFormObject[fieldsNames[i]] = newDate;
}
}
return JSON.stringify(jsonFormObject);
}
finnaly , after getting your jsonForm apply the function on it :
var formData = dojo.formToJson("yourFormID");
formData = formatJsonFormDates(formData,dijit.byId("yourFormID"),["CONTRACT_DATE"],"MM-dd-yyyy");
.
Post a Comment for "Getting Different Date Pattern When Using Dojo.formtojson"