EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: Coder on December 26, 2019, 10:37:19 AM



Title: [SOLVED: added new functionality] datebox getValue as Date object
Post by: Coder on December 26, 2019, 10:37:19 AM
How  get current value from datebox as Date object ?

my formatter is dateFormat(aDate,"dd mmm HH:MM")
and .datebox('getValue') return string in this format ;(


Title: Re: datebox getValue as Date object
Post by: jarry on December 29, 2019, 07:39:47 PM
Please call this code to convert a string to a Date object.
Code:
var db = $('#db');
var opts = db.datebox('options');
var s = db.datebox('getValue');
var d = opts.parser.call(db[0],s);
console.log(d)


Title: Re: datebox getValue as Date object
Post by: Coder on December 31, 2019, 11:32:09 AM
Thnx for Your reply, but:

I use my parser for decode mySQL datetime format

Code:
  function parseMySQLdate(aDate){
var matches = aDate.match(/^\s*(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\s*$/)
, lDate = new Date()
;

if(matches){
var Y = parseInt(matches[1],10)
,   M = parseInt(matches[2],10) - 1
,   D = parseInt(matches[3],10)
,   H = parseInt(matches[4],10)
,   N = parseInt(matches[5],10)
,   S = parseInt(matches[6],10)
;
            lDate.setDate(D);    
            lDate.setMonth(M);    
            lDate.setFullYear(Y);    
   lDate.setHours(H);
   lDate.setMinutes(N);
   lDate.setSeconds(S);

}

return lDate;
}

so it`s not suitable for...
and  db.datebox('getValue'); return string without year (because my formatter is dateFormat(aDate,"dd mmm HH:MM") )

is there a possibility to store date as Date object in date[time]box inside formatter or inside ('setValue')
and get it as Date object from ('getValue') ?

OR maybe add
 .datetimebox('getDate')
 .datetimebox('setDate',DateObject) ?


Title: Re: datebox getValue as Date object
Post by: jarry on January 04, 2020, 03:23:54 AM
This is the extended method 'getDate'.
Code:
$.extend($.fn.datebox.methods, {
getDate: function(jq){
var opts = jq.datebox('options');
var s = jq.datebox('getValue');
var d = opts.parser.call(jq[0],s);
return d;
}
})

The live example is available from http://code.reloado.com/oridat4/edit#preview


Title: Re: datebox getValue as Date object
Post by: Coder on January 07, 2020, 01:43:05 PM
Thnx for reply, and very interesting code

but:
Code:
$('#db').datebox('setValue','2010-11-01 12:30:00')
var d = $('#db').datebox('getDate');
alert(d.toString());

result is Mon Jun 11 2187 00:00:00

if I change parser for mySQL date parse then it didnt work for "dd mmm HH:MM" format. solve for this is use another parserFunction inside getDate method BUT information about year is lost.

Does it exist access to datebox object from inside formatter? For example for set $(datebox).prop('Date',date)
and extract it from getDate method ( return jq.datebox.prop('Date') ) ?


Title: Re: datebox getValue as Date object
Post by: jarry on January 09, 2020, 12:40:35 AM
This is the extended 'setDate' and 'getDate' methods.
Code:
$.extend($.fn.datebox.methods, {
setDate: function(jq, date){
return jq.each(function(){
var opts = $(this).datebox('options');
var value = opts.formatter.call(this, date);
opts.currDate = date;
$(this).datebox('setValue', value);
})
},
getDate: function(jq){
var opts = jq.datebox('options');
if (opts.currDate){
return opts.currDate
}
var s = jq.datebox('getValue');
var d = opts.parser.call(jq[0],s);
return d;
}
})


Title: Re: datebox getValue as Date object
Post by: Coder on January 09, 2020, 04:20:06 AM
thnx for road...

may be last question about datebox in this thread ^)

How to owerride or add  changes to currDate variable when date setted with calendar ?


Title: Re: datebox getValue as Date object
Post by: jarry on January 10, 2020, 02:46:44 AM
The 'getDate' and 'setDate' methods have been included into the newest version. Please download it from the site.

Here is the live example.
http://code.reloado.com/atorub3/edit#preview


Title: Re: datebox getValue as Date object
Post by: Coder on January 10, 2020, 05:08:21 AM
YOU ARE WONDERFUL !   ::) ::) ::)