Change Date Format Who Is Stored In Text Field
I have a common date field having a displayDD-MM-YYYY but the real value stored in database is YYYY-MM-DD. The problem is this value is also transmitted to a remote server inside a
Solution 1:
Use inbuilt javascript functions to build the format you want. PArse the string in to a date object and use the following functions to create your desired format
getDate() -> togetdate
getMonth() -> toget month
getFullYear() -> toget year
Example
//var birth_date = document.getElementById('birth_date');//use birth_date instead of hard coded date//var day = new Date(Date.parse(birth_date));var day = newDate(Date.parse("2013-09-02"));
alert(day.getDate() + "-" + day.getMonth() + "-" + day.getFullYear());
//set valuedocument.getElementById('birth_date').value = day.getDate() + "-" + day.getMonth() + "-" + day.getFullYear();
Solution 2:
Say you have a string var s = '1989-05-06';
. You can get the year, month and day separately like so:
var my_date = s.split('-');
var year = my_date[0];
var month = my_date[1];
var day = my_date[2];
Then, you can display the string below (or organize the day, month and year in any format you would like):
var display_str =''+day+'-'+month+'-'+year;
Solution 3:
You can catch the form submit event and change the value before it is sent:
Let's say you have a form #myForm
:
var form = document.getElementById('myForm');
To catch submission:
try {
form.addEventListener("submit", changeValue, false);
} catch(e) {
form.attachEvent("onsubmit", changeValue); //Internet Explorer 8-
}
Now you can change the desired value inplace.
function changeValue(){
var field = document.getElementById('idOfField');
field.value = field.value.split('-').reverse().join('-');
}
Solution 4:
well, we can do that using simple javascript function. we just need to pass server date(YYYY-MM-DD) and it wll return expected date (DD-MM-YYYY) format.
functionconvertDate(serverDate) {
var dateCollection = serverDate.match(/\d+/g),
year = dateCollection[0],
month = dateCollection[1],
day = dateCollection[2];
return day + '-' + month + '-' + year;
}
Example:-
convertDate('2013-09-02'); // (YYYY-MM-DD) format
output:-
'02-09-2013'//(DD-MM-YYYY) format
Hope this will help you...
Post a Comment for "Change Date Format Who Is Stored In Text Field"