Aeglasin
Aeglasin

Reputation: 165

sap.ui.core.format.DateFormat.format give wrong date

For one Client, and only when switch the browser to english, the following code:

convertTimeToDate: function(dateobj) {


            if ((dateobj == undefined) || (dateobj == null)) {
                return "";
            }


            var dateFormat = sap.ui.core.format.DateFormat.getDateInstance({
                pattern: "dd. MMM YYYY"
            });

            var dateFormatted = dateFormat.format(dateobj);

           return dateFormatted;
},

returns when inputing the highdate '9999-12-31' the datestring '31. Dec 10000' what could be the problem? It is reproducible only on the maschine of this one person, but for her it happens ALWAYS.

Upvotes: 1

Views: 4911

Answers (3)

slkorolev
slkorolev

Reputation: 6001

To avoid problems with timezones, especially in UI5 apps in HR, I decided to send/receive dates and times in ISO format as strings in my recent developments, it guarantees that a user in browser will see the same value as in the SAP back-end system.

To make sure you use these values both for input and output it is recommended to implement a Custom Data Type.

Suppose you have an sap.m.DateRangeSelection which accepts JavaScript Date objects as dateValue and secondDateValue, and you want bind these properties to a model, where properties like startDate and endDate are date strings of yyyy-MM-dd pattern. In this case a sample custom data type can be like this:

sap.ui.define([
	"sap/ui/model/SimpleType",
	"sap/ui/core/format/DateFormat"
], function(SimpleType, DateFormat) {
	"use strict";
	return SimpleType.extend("MyProject.model.ISODateType", {
		
    /**         
		 * Converts JavaScript Date object to ISO date string    
		 *         
		 * @public         
		 * @param {string} sISO the ISO value of the formatted property         
		 * @returns {string}     
		 */parseValue: function(oDate) {
			var oFormat = DateFormat.getDateInstance({
				pattern: "yyyy-MM-dd"
			});
			return oFormat.format(oDate);
		},
		/**         
		 * Produces JavaScript Date object from ISO date string    
		 *         
		 * @public         
		 * @param {string} sISO the ISO value of the formatted property         
		 * @returns {string}     
		 */
		formatValue: function(sValue) {
			return new Date(sValue);
		},
		/**         
		 * Validates the value to be parsed (should be ISO date string)        
		 *         
		 * @public         
		 * no client side validation is required        
		 * @returns {boolean} true         
		 */
		validateValue: function(sValue) {
			var sPattern = /(\d{4})-(\d{2})-(\d{2})/;
			return sValue.match(sPattern);
		}
	});
});

And the binding in an XML View can look like this:

<DateRangeSelection
                width="30%"
                dateValue="{
                    path: '/startDate',
                    type: 'MyProject.model.ISODateType'
                }"
                secondDateValue="{
                    path: '/endDate',
                    type: 'MyProject.model.ISODateType'
                }"
                change="onDateIntervalChange"/>

Upvotes: 3

Onur Ak&#231;a
Onur Ak&#231;a

Reputation: 101

I was plagued by the same 10k date. My pattern was {pattern: 'MMMM Y'}. Changing it to {pattern: 'MMMM yyyy'} solved the problem. I cannot find any documents on the difference between 'Y' and 'y' though.

Upvotes: 2

dotchuZ
dotchuZ

Reputation: 2651

just an idea: the user did something (whatever) in his browser and timezone is somehow different for him/her. did you try passing a fixed timezone during fomatting? it looks like some seconds are added .. maybe some converting offset or whatever.

https://openui5.hana.ondemand.com/#/api/sap.ui.core.format.DateFormat/methods/sap.ui.core.format.DateFormat.getDateInstance @since 1.34.0 contains pattern symbols (e.g. "yMMMd" or "Hms") which will be converted into the pattern in the used locale, which matches the wanted symbols best. The symbols must be in canonical order, that is: Era (G), Year (y/Y), Quarter (q/Q), Month (M/L), Week (w/W), Day-Of-Week (E/e/c), Day (d/D), Hour (h/H/k/K/j/J), Minute (m), Second (s), Timezone (z/Z/v/V/O/X/x) See http://unicode.org/reports/tr35/tr35-dates.html#availableFormats_appendItems

something like: https://gist.github.com/bvanzyl/b3b64306a7363f0b3e043608b48d8326

formatDateStr: function(data){
             if(data === null || data === undefined){
                    return data;
             }else{
                    var oDate = new Date(data);
                    var iLocalTime = oDate.getTime();
                    // Berlin timezone = UTC+1 = 1hr = 60min ahead 
                    // 60min * 60000 for time in milliseconds
                    var iBerlinOffset = 60 * 60000;
                    var iLocalOffset = oDate.getTimezoneOffset() * 60000;
                    var iBerlinTime = iLocalTime + iLocalOffset + iBerlinOffset;
                    oDate.setTime(iBerlinTime);
                    var dateFormat = sap.ui.core.format.DateFormat.getDateInstance({pattern : "MMM, dd YYYY" });  
                    var dateFormatted = dateFormat.format(oDate);
                    return  dateFormatted;
             }
       }
   };

Upvotes: 1

Related Questions