Customer Portal

Get Involved. Join the Conversation.

Topic

    Pramod Vasudeva Murthy
    Setting a Date field in CP - Very tiring Process !!!:(Answered
    Topic posted May 28, 2016 by Pramod Vasudeva MurthyBlack Diamond: 60,000+ Points 
    729 Views, 23 Comments
    Title:
    Setting a Date field in CP - Very tiring Process !!!:(
    Content:

    Hi All,

    This has been playing games with me from past 3 days; still not able to crack.

    I have a Custom object's date field called TestDate.

    I am getting date in this format from the form - 12/02/2015(dd/mm/yyyy). I have tried following methods and they all seem to either behave/work properly for one set of dates and work unlike.

    Sometimes this field is set to 01/01/1970. I have fought enough with this and finally gave upindecision.

    Please suggest an alternative.

    Thanks in advance,

    - VIP

    Version:
    Feb 2016
    Code Snippet:

    Best Comment

    Anuj Behl

    Hi Pramod,

    Thanks for trying it out. Now that you are able to get a working date format accepted by your custom field, we can work to fix the variation. You'll need to send the timezone along with the date while passing it to strtotime() method. Please check Rajan's jsfiddle above, you can use that to convert your date into ISO formatted date with timezone and pass that to strtotime. This way there won't be any variations.

    Comment

    • Rajan Davis

      https://jsfiddle.net/ob5zLroh/

      I modifed a jsfiddle to show how you can get the date parsed using the jQuery UI Datepicker. There is a built in method to retrieve the date, you can chain a .toISOString() method at the end and your date will be parsed in the correct format.

    • Pramod Vasudeva Murthy

      Hi Anuj,

      I have used the one you suggested: Instance is of Australia/Melbourne Timezone.

      $datetime = $test_date;

      logMessage("Test date");
      logMessage($datetime);

      $formatted_date = str_replace('/','-',$datetime);
      $time = strtotime($formatted_date);
      $web_form1->TestDate = $time;

      It is setting one day before(may be due to change in Interface setting), I had modified my code to this:

      $datetime = $test_date;

      logMessage("Test date");
      logMessage($datetime);

      $formatted_date = str_replace('/','-',$datetime);
      $time = strtotime("+1 day", strtotime($formatted_date));
      $web_form1->TestDate = $time;

      which sets the date correctly.

      But the funny thing is - If I set the date to be today it is setting to tomorrows date. Not sure how to tackle this.

      - VIP

    • Anuj Behl

      Hi Pramod,

      Thanks for trying it out. Now that you are able to get a working date format accepted by your custom field, we can work to fix the variation. You'll need to send the timezone along with the date while passing it to strtotime() method. Please check Rajan's jsfiddle above, you can use that to convert your date into ISO formatted date with timezone and pass that to strtotime. This way there won't be any variations.

    • Pramod Vasudeva Murthy

      Hi Anuj/Rajan/Barilito/Narendra,

      Thanks for your inputs, I really appreciate your interest in resolving this query. I found this way to be working, which was suggested by Anuj. Hence marking his answer as Right Solution.(No offense to anyone smiley)

      $datetime = $test_date;
      date_default_timezone_set("Australia/Melbourne");
      $formatted_date = str_replace('/','-',$datetime);
      $time = strtotime($formatted_date);
      $web_form1->TestDate = $time;

      - VIP

    • Barrilito van Dijk

      Hi Pramod,

      Nice to see you got it working! And thank you for sharing the solution here on the community.

      It was a good and interesting discussion with a good result smiley

      Best regards.

    • Dietrik

      To add to the discussion, the php strtotime function needs the right input as can be read on php.net http://php.net/manual/en/function.strtotime.php:

      Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.

      And

      To avoid potential ambiguity, it's best to use  DateTime::createFromFormat() when possible.

    • Rajan Davis

      I think this was/is a really good discussion about a fairly difficult topic. I don't think there is a _one_true_way_ of working with Date/Time strings and I am glad to see that this issue was resolved. At least there are several documented solutions when there previously were none.

      My preference has been to parse Date/Time objects using Javascript because it is easier for me to understand what data is being passed into a controller. I can throw a console.log() and inspect my code and focus the controller to validating and handling data without any added behavior.

    • Anuj Behl

      I agree with you, Rajan. I also try to keep things which are related to data formats and validation in javascript. No doubt it is easier to test but easier to manage as well, if one needs to update the validation in future. A fruitful discussion indeed.