EasyUI Forum

General Category => General Discussion => Topic started by: Pierre on April 14, 2014, 05:57:05 AM



Title: Date formatting
Post by: Pierre on April 14, 2014, 05:57:05 AM
Hello all
I have this function (for date formatter filed):

Code:
...
 <class="fitem"><label>Date</label><input class="easyui-datebox" name="boq_date" data-options="formatter:dateformatter,parser:dateparser">
...

and this function:

Code:

function dateformatter(date)

  var d = date.getDate(); 
  var m = date.getMonth()+1; 
  var y = date.getFullYear(); 
  return (d<10?('0'+d):d)+'.'+(m<10?('0'+m):m)+'.'+y; 


I need this format:
14.04.2014 instead of 04/14/2014
and it does not work. I returns only today's date.
How to format datebox correctly so I can have 14.04.2014 displayed?
Thank you.


Title: Re: Date formatting
Post by: stworthy on April 14, 2014, 07:40:39 AM
Please try this:
Code:
<input class="easyui-datebox" data-options="formatter:myformatter,parser:myparser"></input>
<script type="text/javascript">
function myformatter(date){
var y = date.getFullYear();
var m = date.getMonth()+1;
var d = date.getDate();
return (d<10?('0'+d):d) + '.' + (m<10?('0'+m):m) + '.' + y;
}
function myparser(s){
if (!s) return new Date();
var ss = (s.split('\.'));
var d = parseInt(ss[0],10);
var m = parseInt(ss[1],10);
var y = parseInt(ss[2],10);
if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
return new Date(y,m-1,d);
} else {
return new Date();
}
}
</script>


Title: Re: Date formatting
Post by: Pierre on April 15, 2014, 12:05:11 AM
It works perfect, thank you.


Title: Re: Date formatting
Post by: Pierre on April 18, 2014, 02:13:43 AM
Stworthy
could you please tell me (or someone else) how to use your example to return US date?
I use
data-options="formatter:myformatter,parser:myparser"
for all date fields and when I need European date, your example works OK but when I need US date, I need to somehow return 04/17/2014 instead of 17.04.2014
thanks for any hint.


Title: Re: Date formatting
Post by: stworthy on April 19, 2014, 02:12:33 AM
If you want to use '04/17/2014' format, don't use the custom 'formatter' and 'parser' functions. So <input class="easyui-datebox" data-options="formatter:myformatter,parser:myparser"> will return '04/17/2014' format and <input class="easyui-datebox"> will return the default US format.


Title: Re: Date formatting
Post by: Pierre on April 19, 2014, 10:38:31 PM
Thank you Stworthy, I understand that, but I meant to keep <input class="easyui-datebox" data-options="formatter:myformatter,parser:myparser"> and to change 'formatter' and 'myparser' (JavaScript) side instead of HTML side (there where I declare them).
It it is not possible, then OK - I would use as you suggested.
Thanks.