Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Date functions

    I want to only display the date and strip the time portion from Date.toNormalDate() if the time is 12:00:00 AM.

    How can I do this? Is there custom date format templates or can I only use the function references as listed in DateDisplayFormat

    #2
    Here's how I solved it:

    Code:
    	var dt = Date.parseInput(value);
    	var ret = dt.toNormalDate();
    	ret = ret.replace(/ 12:00:00 AM/,"");
    	return ret;

    Comment


      #3
      Hi tgochenour,
      Your approach will work fine.

      As you noted we also supply a set of standard other formats you can use with the toNormalDate() method.

      For a truly custom format such as the one you describe there are a couple of options:

      You can pass a custom formatter function directly into the toNormalDate() method. This function will be executed in the scope of the date object.

      Code:
      var myDate = new Date();
      function customFormatter () {
            return this.getFullYear() + "." + (this.getMonth() +1) + "." +
                     this.getDate();
      }
      isc.warn(myDate.toNormalDate(customFormatter));

      Or you can set your custom formatter function up as the standard normal formatter via Date.setNormalDisplayFormat(), like this:

      Code:
      function customFormatter () {
            return this.getFullYear() + "." + (this.getMonth() +1) + "." +
                     this.getDate();
      }
      Date.setNormalDisplayFormat(customFormatter);
      var myDate = new Date();
      
      isc.warn(myDate.toNormalDate(customFormatter));
      Of course if you take this approach you will not be able to call date.getNormalDate() in this formatting function, or you'll end up with an infinitely recursive loop. If you did want to take this approach you'd want to call this.toLocaleString() (the default toNormalDate formatter) from within your formatter function instead.
      However if you're not coping this in multiple places in your app, it's probably not necessary to rework your approach.

      Comment


        #4
        In your second example, did you mean to say:

        Code:
        Date.setNormalDisplayFormat(customFormatter);
        var myDate = new Date();
        
        isc.warn(myDate.toNormalDate(customFormatter));
        or should this read:

        Code:
        Date.setNormalDisplayFormat(customFormatter);
        var myDate = new Date();
        
        isc.warn(myDate.toNormalDate());

        Comment


          #5
          Sorry - you're right I meant to say

          isc.warn(myDate.toNormalDate());

          No need to pass the formatter function into the toNormalDate() method as a parameter since it's already been set up as the default.

          Thanks for catching that

          Comment

          Working...
          X