EasyUI Forum
April 29, 2024, 09:36:35 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Date formatting  (Read 17603 times)
Pierre
Sr. Member
****
Posts: 439


View Profile Email
« 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.
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 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>
Logged
Pierre
Sr. Member
****
Posts: 439


View Profile Email
« Reply #2 on: April 15, 2014, 12:05:11 AM »

It works perfect, thank you.
Logged
Pierre
Sr. Member
****
Posts: 439


View Profile Email
« Reply #3 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.
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #4 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.
Logged
Pierre
Sr. Member
****
Posts: 439


View Profile Email
« Reply #5 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.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!