Announcement

Collapse
No announcement yet.

Assigning a date to a date time field

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Assigning a date to a date time field

    Hi, your advice would be appreciated here.

    On my tasks I have a "Date Due" field which is of type date time (I believe this is the default configuration).

    In script I want to assign the Date Due of now plus 3 days. I want the result to just be the date without the time.

    How do I do this? It sounds simple but I cannot work it out.

    Thanks

  • #2
    Hello,

    this is maybe a easy solution

    Comment


    • #3
      Thanks for the suggestion, I really need the ability to do it in script though because the +3 varies depending on many other things.

      Comment


      • esforim
        esforim commented
        Editing a comment
        Formula would work but it very ugly when you want complexity. From what I seen of Workflow that should be doable but it is a Paid extension.

        --
        I'm leaving this as a comment here because it not really a good solution.

    • #4
      Hello,
      you can create un custom folder entityDefs .. a task.json with somethink like this : so dateDue go from type dateTime to date

      PHP Code:
      {
      "fields": {

      "dateDue": {
      "notNull"false,
      "type""date",
      "default"null,
      "after"null,
      "before"null,
      "isCustom"true
      }

      Comment


      • #5
        Hi esforim thanks for the comment. When you say "Formula would work but it very ugly when you want complexity" can you give an example?

        Thanks

        Comment


        • #6
          Ah I read your post again, it seem like you want to hide the "Time" section? I don't think you can do that through GUI, why not just create a "Date" field instead?

          To answer your question about formula, I did a test of it and it seem to work fine.

          As an experiment, I created two field, one is a Date-Time field call datedue. You don't have to make two.
          one is a Date field call datedue2

          Then I create this formula for my Task

          datedue=datetime\addDays(dateEndDate, 3);
          datedue2=datetime\addDays(dateEnd, 2)

          Let break it down.
          datedue = field name
          datetime\addDays() = formula
          dateEndDate or dateEnd = default field available for Task, dateEnd is Date-Time field, dateEndDate is a Date field (All-day mode).
          3 or 2 = How many days I want to add, I want add 3 and 2, I can also do -1 (yesterday) if I want to.
          ) = close off the formula
          ; = to allow for new formula to be written

          Please see all the formula here: https://docs.espocrm.com/administrat...mula/#datetime
          and See
          https://docs.espocrm.com/administration/formula/#datetimeadddays


          I also use this variation too: datedue=datetime\addDays(dateEnd, 1)
          Last edited by esforim; 09-02-2020, 08:24 AM.

          Comment


          • esforim
            esforim commented
            Editing a comment
            Now when I say ugly, this what I mean (example written on top of my head only):

            ifThenElse(status="Not Started",+1 days,
            ifThenElse(status="Started",+5 days,
            ifThenElse(status="Completed",Description="Send invoice")

        • #7
          Thanks, for the detail. I don't want to hide the time part of the field, I just want the time portion to be unassigned. When using the user interface I can specify a date then optionally a time. The time segment can be left empty. This works in certain ways when sorting that I want.

          In script I currently have
          Code:
          dateEnd = datetime\addDays(datetime\now(), 3);
          This sets the due date to be now plus 3 days. If now is mid day then the due date is mid day, 3 days from now.

          I want the script equivalent of setting the time section of that field to be empty within the user interface.

          Thanks

          Comment


          • #8
            I see your dilemma, but can't help you!

            If that the case then you either need to do some sort of "All Day" conversation (I don't know how), or to make it a time pre-working hours (e.g. set it to be at 6am or 8am for example).

            It convert the time to be 5:01pm (end of the day).

            From all the formula I see, maybe if you somehow combine it with this: datetime\hour
            It might work?

            As for how, like I said first sentence, can't help you!

            Comment


            • esforim
              esforim commented
              Editing a comment
              Maybe something like this? Logically I think something like this.

              dateEnd = datetime\hour(datetime\addDays(datetime\now(), 3),8)

              Realistically, you probably end up with Error 500. Looking at the formula again, that formula was stupid of me;

              Maybe this (might) work:

              dateEnd = datetime\closest(datetime\addDays(datetime\now(), 3, 'time', '17:01')
              Last edited by esforim; 09-02-2020, 08:39 AM.

          • #9
            I do think that the solution that item referred to is the simplest and correct way to get today + 3 days as the default value.

            BUT if I correctly understood, your problem is the type of the field, you need date and not datetime,

            Okay, you have two action to take with to accomplish this:

            go to the file: custom/Espo/Custom/Resources/metadata/entityDefs/Task.json

            Update the "default" value manually from :

            javascript: return this.dateTime.getDateTimeShiftedFromNow(3, 'days', 15);
            TO
            javascript: return this.dateTime.getDateShiftedFromToday(3, 'days');

            Change the type from dateTime to date (this is optional)

            Click image for larger version

Name:	Screen Shot 2020-09-02 at 10.27.10 AM.png
Views:	1013
Size:	120.6 KB
ID:	62207
            CEO of Eblasoft
            EspoCRM Expert since 2014
            Full Stack Web Developer since 2008
            Creator of Numerous Successful Extensions & Projects​

            Comment


            • #10
              Thanks everyone, esforim, item and eymen-elkum for the help on this. In digging further it seems that they built this into the product already. On a Task are 2 fields of interest; dateEnd and dateEndDate. They both map onto the same database field. The first one is of type date time, the second is of type date. If I assign to the 2nd one everything works well.

              Thanks all.

              Comment

              Working...
              X