EllisLab text mark
Advanced Search
     
date picker code problem
Posted: 20 June 2012 05:18 PM   [ Ignore ]
Joined: 2012-03-16
10 posts

I needed to get rid of the timestamp appended to the datepicker field and found a topic that solved exactly that
http://ellislab.com/forums/viewthread/207191/

by modifying this file and line
expressionengine > modules > safecracker > libraries > safecracker_lib.php

if ($this->datepicker)
  
{
   $this
->EE->javascript->output('$.datepicker.setDefaults({dateFormat:$.datepicker.W3C+EE.date_obj_time});');
  

to this

if ($this->datepicker)
  
{
   $this
->EE->javascript->output('$.datepicker.setDefaults({dateFormat: "mm-dd-yy"});');
  

That works great but when i submit my safecracker form it fails validation and does not save the field data. Does anyone know of a way to modify the code to overide this validation for the date fields?

edit: im guessing its failing validation because the save works until i modify this line of code, then the form stops saving date field information

 
Posted: 03 July 2012 08:20 PM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2010-07-15
426 posts

You are correct the date field is not passing validation. It must be YYYY-MM-DD HH:MM PM.

Your best solution is to implement a dummy input with an instance of the datepicker that you control and then use the onSelect callback to populate a hidden input with the correctly formatted date for EE. See below:

{exp:safecracker channel="xxx" datepicker="no"}
 
<div>
  <
label for="entry_date_picker">Entry Date</label>
  <
input type="text" name="my-fake-date" value="" id="entry_date_picker" />
  <
input type="hidden" name="entry_date" value="" id="entry_date" />
 </
div
 <
div>
  <
input type="submit" value="Submit">
 </
div>
{/exp:safecracker}

[removed]
 jQuery
(function() {
  
  
var entry_date = $('#entry_date');
  var 
date Date.parse(entry_date.val());
  if (
date{
   
$('#entry_date_picker').val((date.getMonth() + 1)+'/'+date.getDate()+'/'+date.getFullYear());
  
}
  
  jQuery
("#entry_date_picker").datepicker({
   dateFormat
"mm-dd-yy",
   
onSelect: function(dateinst{
    
var day = (inst.currentDay.length == 1) ? "0"+inst.currentDay inst.currentDay,
     
month = (inst.currentMonth.toString().length == 1) ? "0"+(inst.currentMonth 1) : (inst.currentMonth 1),
     
year inst.currentYear;
    
//YYYY-MM-DD hh:mm PM
    
entry_date.val(year+'-'+month+'-'+day+' 12:00 AM');
   
}
  }
);
  
 
});
[removed] 
 Signature 

ExpressionEngine Development | 21purple Web Studios, LLC or on Twitter @th3mus1cman

 
Posted: 31 August 2012 01:44 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2010-10-17
181 posts

So, I’m trying to set this up for using a format like dd.mm.yy - possible? And if so, how!? smile

 
Posted: 31 August 2012 02:05 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2010-07-15
426 posts

Not sure if it is possible.  Try putting dd.mm.yy for dateFormat option.

jQuery("#entry_date_picker").datepicker({
   dateFormat
"dd.mm.yy",
   
onSelect: function(dateinst{
    
var day = (inst.currentDay.length == 1) ? "0"+inst.currentDay inst.currentDay,
     
month = (inst.currentMonth.toString().length == 1) ? "0"+(inst.currentMonth 1) : (inst.currentMonth 1),
     
year inst.currentYear;
    
//YYYY-MM-DD hh:mm PM
    
entry_date.val(year+'-'+month+'-'+day+' 12:00 AM');
   
}
  }
); 
 Signature 

ExpressionEngine Development | 21purple Web Studios, LLC or on Twitter @th3mus1cman

 
Posted: 31 August 2012 02:08 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2010-10-17
181 posts

Nope, doesn’t work, but thanks for your help!

 
Posted: 18 October 2012 03:39 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2004-04-14
109 posts
the3mus1can - 31 August 2012 02:05 AM

Not sure if it is possible.  Try putting dd.mm.yy for dateFormat option.

That works for me. Thanks.

Only if the selected month is October I get a 3-digit month as a result like this

2012-010-17 12:00 AM

November and December are then again 2 digits.

I am not that familiar with Javascript. Any idea how that could be fixed? I guess it must be here

month = (inst.currentMonth.toString().length == 1) ? "0"+(inst.currentMonth 1) : (inst.currentMonth 1),
       
year inst.currentYear

Ah, I understand that js handles month from 0 to 11. In the above code October comes as 9 gets a leading zero and 1 is added to it. That results in 010.

How should that be rewritten?

 Signature 

Werner Gusset | http://www.octave2media.ch |
Translator German language files for EE & add-ons

 
Posted: 18 October 2012 01:22 PM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2010-07-15
426 posts

This should work:

month = (inst.currentMonth 9) ? "0"+(inst.currentMonth 1) : (inst.currentMonth 1), 
 Signature 

ExpressionEngine Development | 21purple Web Studios, LLC or on Twitter @th3mus1cman

 
Posted: 19 October 2012 12:27 AM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2004-04-14
109 posts

Yes, thank you

this works.

 Signature 

Werner Gusset | http://www.octave2media.ch |
Translator German language files for EE & add-ons