Announcement

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

    Date.setInputFormat issue (date as dateTime)

    Hi Isomorphic,

    We just upgraded our front-end to SmartClient_SC_SNAPSHOT-2011-11-24_LGPL (v8.1 from 8.0) recently.

    Everything seems to be fine except the dates handling. Before updating, using DatePickers (fields of type 'date') we used to get dates formatted like 'MM/DD/YYYY'... After the upgrade we started getting dates formatted like 'MM/DD/YYYY 12:00:00' which breaks our entire date handling across the app.

    Here is an excerpt of the way we set dates:

    Code:
    //--------------------------------------------------------------------------------------------------------------------------------
    // Set global date formatting rules
    //--------------------------------------------------------------------------------------------------------------------------------
    Date.setShortDisplayFormat(DateUtils.getUiFormatDateString);
    Date.setNormalDisplayFormat(DateUtils.getUiFormatDateString);
    Date.setInputFormat('MDY');
    
    var DateUtils = isc.defineClass("DateUtils", "Class");
    
    DateUtils.addClassProperties
    ({
    	//--------------------------------------------------------------------------------------------------------------------------------
    	/**
    	 * Returns the UI date string representation of a given date object. Returns null if supplied object is not a date object.
    	 *
    	 * @param {Date} date Date for which to obtain the UI representation
    	 * @param {String} dateTimeDelimiter Will be added between the date and time portion of UI representation 
    	 * @return {String} UI string representation for given date
    	 */
    	//----------------------------------------------------------------------------------------------------------------------------
    	getUiFormatDateString : function(date, dateTimeDelimiter)
    	{
    		if (this != null && isA.Date(this))
    			date = this;
    		return DateUtils.getFormattedDate(date, dateTimeDelimiter);
    	},
    	
    	//----------------------------------------------------------------------------------------------------------------------------
    	/**
    	 * Returns a formatted string representation of a given date object. 
    	 * @param {Date} date Date for which to obtain the ANSI representation
    	 * @param {String} dateTimeDelimiter Will be added between the date and time portion of the formatted representation 
    	 * @return {String} Ansi string representation for given date
    	 */
    	//----------------------------------------------------------------------------------------------------------------------------
    	getFormattedDate : function(date, dateTimeDelimiter)
    	{
    		if (dateTimeDelimiter == undefined)
    			dateTimeDelimiter = ' ';
    
    		if (!isA.Date(date))
    			return null;
    
    		var strYear = '' + date.getFullYear();
    	
    		var strMonth = '' + (date.getMonth() + 1);
    		if (strMonth.length != 2)
    			strMonth = '0' + strMonth;
    
    		var strDay = '' + date.getDate();
    		if (strDay.length != 2)
    			strDay = '0' + strDay;
    
    		var strDate;
    		strDate =  strMonth + '/' + strDay + '/' + strYear;
    
    		if (DateUtils.isDateTime(date))
    		{
    			var strHours = '' + date.getHours();
    			var strMinutes = '' + date.getMinutes();
    			var strSeconds = '' + date.getSeconds();
    
    			strDate += dateTimeDelimiter;
    
    			if (strHours.length < 2)
    				strDate += '0';
    
    			strDate += strHours;
    			strDate += ":";
    
    			if (strMinutes.length < 2)
    				strDate += '0';
    
    			strDate += strMinutes;
    			strDate += ":";
    
    			if (strSeconds.length < 2)
    				strDate += '0';
    
    			strDate += strSeconds;
    		}
    
    		return strDate;
    	},
    	
    	//--------------------------------------------------------------------------------------------------------------------------------
    	/**
    	 * Returns whether a date object is a date/time. Assumes a date/time as soon as one of the date's time components
    	 * is set to a non-zero value.
    	 *
    	 * @param {Date} date Date to evaluate
    	 * @return {Boolean} true when dateTime, false otherwise
    	 */
    	//----------------------------------------------------------------------------------------------------------------------------
    	isDateTime : function(date)
    	{
    		if (!isA.Date(date))
    			return false;
    
    		return date.getHours() || date.getMinutes() || date.getSeconds() || date.getMilliseconds() ? true : false;
    	}
    });
    As you can see we determine whether a date is dateTime by looking into the getHours(), getMinutes().... methods.

    Any insights on that?

    Thank you in advance,
    Last edited by alirioperez; 5 Dec 2011, 07:27.

    #2
    That's not a valid way to determine if something is a datetime. We now represent logical "date" values as noon, because there are countries where midnight does not exist on certain days due to daylight savings time.

    Comment


      #3
      Note even aside from this change it's not a good idea to use such a check because a datetime value that happens to be midnight exactly will be formatted as a date.

      Comment


        #4
        Originally posted by Isomorphic
        That's not a valid way to determine if something is a datetime. We now represent logical "date" values as noon, because there are countries where midnight does not exist on certain days due to daylight savings time.
        Thanks for the answers. But, in your opinion, what is the best way to determine if a "date" type is a date or a dateTime

        Thank you very much in advance

        Alirio

        Comment


          #5
          There is no way to determine it from an actual date/datetime value - that's fundamentally ambiguous. The framework does it by always having the field definition or similar metadata whenever the determination needs to be made.

          Comment


            #6
            I see, but how I'm supposed to reach such metadata or field definition when our low level NormalDisplayFormat function is called?

            When you say "The framework does it by always having the field definition or similar metadata..", it means that at low level you know where the date is coming from?

            Comment


              #7
              You have access to the same metadata. You've simply factored things incorrectly such that you are calling the same formatter function for both date and datetime fields. Wherever you're installing formatters, you either need to install different formatters for date vs datetime fields, or you can use a single formatter if you can find a way to pass type information.

              Comment


                #8
                Thank you guys, we re-factored a little bit and everything looks fine.

                Comment


                  #9
                  ok...thank you very much... the following code accepts now dd.MM.yyyy and dd.MM.yyy HH:mm... ".setInputFormat( "DMY" );" was the key!!!


                  For all who have the same problem.... The following code shows how to set date TimeZone systemwide to the german defaults.
                  Code:
                  // Sets the date and dateTimeFormats
                          final String DATE_TIME_PATTERN = "dd.MM.yyyy HH:mm";
                          final String DATE_PATTERN = "dd.MM.yyyy";
                          final DateTimeFormat dateFormat = DateTimeFormat.getFormat( DATE_PATTERN );
                          final DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat( DATE_TIME_PATTERN );
                  
                          // Creates a DisplayFormatter for dates
                          DateDisplayFormatter dateFormatter = new DateDisplayFormatter()
                          {
                              @Override
                              public String format( Date date )
                              {
                                  String form = dateFormat.format( date );
                                  return form;
                              }
                          };
                  
                          // Creates a DisplayFormatter for dateTimes
                          DateDisplayFormatter dateTimeFormatter = new DateDisplayFormatter()
                          {
                              @Override
                              public String format( Date date )
                              {
                                  String form = dateTimeFormat.format( date );
                                  return form;
                              }
                          };
                  
                          // Sets the standard formats
                          DateUtil.setNormalDateDisplayFormatter( dateFormatter );
                          DateUtil.setShortDateDisplayFormatter( dateFormatter );
                          DateUtil.setShortDatetimeDisplayFormatter( dateTimeFormatter );
                          
                          // Sets how dates will be parsed after the user entered a new date ( D= day, M=Month, Y=Year)
                          DateUtil.setInputFormat( "DMY" );
                  
                          // Sets the standard timezone
                          TimeZoneConstants timeZoneConstants = GWT.create( TimeZoneConstants.class );
                          TIMEZONESERVER = TimeZone.createTimeZone( TimeZoneInfo.buildTimeZoneData( timeZoneConstants.europeBerlin() ) );
                  Last edited by andyx1975; 11 Sep 2015, 03:22.

                  Comment

                  Working...
                  X