Announcement

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

    Date Calculation on Client side

    Hi,

    I am trying to do a date calculation on the client side.
    Whenever a field changes, I need to add it's value to a DateItem.

    For example:

    my dateitem has the value of 15/07/2009 and my integeritem is 10.
    the new dateitem value should become 25/07/2009.

    I was able to do that in gwt-ext using the DateUtil.add method.

    Is there any way to do this in SmartGWT?

    Thank you,

    #2
    Any updates on this??

    Thank you,

    Comment


      #3
      Halabe,
      You should include a js library like this or datejs and add the appropriate JSNI method for adding dates. Alternatively you can just use the relevant code from datejs within your JSNI method since datejs is MIT licensed. This is effectively what gwt-ext does.

      Both these js libraries have very comprehensive date functions including adding dates.

      Sanjiv

      Comment


        #4
        Is there an update to this? Any better support for simple date calculations either in GWT or SmartGWT at this point? If not, any examples of the JSNI needed to use one of these external libraries?

        Comment


          #5
          I also have a related question. Is there any way to create a client side validation to insure that one date field is less than or equal to another date field in the same record?

          Comment


            #6
            You would do this with a Custom Validator - note the getRecord() API.

            Comment


              #7
              Thanks. Any pointers on the latest best practice for doing date arithmetic in a SmartGWT app?

              Comment


                #8
                There's really not much to share. Take a look at Google's docs on how to use JSNI and look over the date arithmetic you need to do and whether datejs has support for it.

                You just end up adding some simple functions like this:

                Code:
                public static Date addDays(Date date, int days) {
                    return new Date(date.getTime() + days * 1000 * 60 * 60 * 24);
                }
                .. but inside JSNI blocks.

                Comment


                  #9
                  Originally posted by Isomorphic
                  .. but inside JSNI blocks.
                  Why isn't possible to do it directy in GWT as in your code? -just wondering-

                  I've tried this
                  Code:
                  Date today=new Date();
                  long acceptableLimit=today.getTime()+30*1000*60*60*24;//add 30 days
                  Date acceptableLimitDate=new Date(acceptableLimit);
                  but didn't work, getTime() seems fine but the add operation behaves weird, do I have to cast to long prior to add values or something? or it just isn't possible and we must do it through JSNI?

                  thanks!

                  Comment


                    #10
                    I ended up using JSNI -extracted from DateUtil class in GWT Ext- although you should be aware of the problems with handling Dates on client side as explained here

                    Code:
                    package your.package
                    
                    public static class DateUtils{
                    		
                    	protected DateUtils() {
                    	}
                    
                    	public static Date createDate(double time){
                    		return new Date((long) time);
                    	}
                    	public static double getTime(Date date){
                    		return date.getTime();
                    	}
                    	
                    	public static native Date currentDate()/*-{
                    		var time=new $wnd.Date();
                    		var today=new $wnd.Date(time.getFullYear(),time.getMonth(),time.getDate(),0,0,0,0);
                    		return @your.package.DateUtils::createDate(D)(today.getTime());
                    	}-*/;
                    	
                    	public static native Date addDays(Date date,int days)/*-{
                    		var millis=@your.package.DateUtils::getTime(Ljava/util/Date;)(date);
                    		var jsDate= new $wnd.Date(millis+days*1000*60*60*24);
                    		return @your.package.DateUtils::createDate(D)(jsDate.getTime());
                    	}-*/;
                    }
                    Last edited by cirovladimir; 27 Aug 2013, 12:02.

                    Comment

                    Working...
                    X