I am doing date format using this function
// DateFormat required from "sap/ui/core/format/DateFormat"
DateFormat.getDateTimeInstance({ pattern: "yyyy-MM-dd HH:mm" });
The format is working fine in "DE" but it is returning wrong value in "EN". In both languages, the time is 24 hour. What I need is:
- In English, the time text should contain AM or PM.
- In German, the time text should be in the 24h format.
How can this be achieved?
Target UI5 version: 1.108
According to the Unicode's Date Field Symbol table:
HH
corresponds to the "24-hour-cycle format"hh
corresponds to the "12-hour-cycle format"jj
"is the only way to [...] request a locale's preferred time cycle type (12-hour or 24-hour)."If not required otherwise, I'd strongly discourage from hardcoding the
pattern
in the format options in order to let the framework format the date and time depending on the user locale. You can use theformat
option instead to specify which symbol to incorporate (e.g.jj
instead ofhh
orHH
) when formatting the date time value.Run this snippet to see the result:
Given today at 23:00:00Z:
API reference:
sap/ui/core/format/DateFormat.getDateTimeInstance
Same can be achieved in property binding within XML declaratively via
type
andformatOptions
.API reference:
sap/ui/model/odata/type/DateTimeOffset
Documentation (must-read): Dates, Times, Timestamps, and Time Zones
Documentation: Formatting, Parsing, and Validating Data — Data Binding (in XML)