![]() This is the case where the new formatter API is trying to improve. The formatter means to do much more than that. All you need is to specify what field (day, month, year) you want to show and leave the implementation detail to the system, e.g., order and format.įormat user-facing string is a simpler operation, but it was build right into the more complicated API. This is totally based on their preference, and we shouldn't try to assume or hard code these values.Īs you can see, this kind of conversion doesn't need a lot of configuration or explicit setting. Some users might interpret 6/25/21 in a dd/MM/yy format, while some might interpret it as MM/dd/yy or else. It will use the default value set in the device and the user's setting, which is the behavior we want. We don't need to specify the locale, time zone, and calendar in this case. In this example, we format the current date into user-facing format. Let's find out the reason for the improvement. This is the case that the new API wants to improve. We convert data to a string intended for users to read. I use the format dd/MM/yyyy to demonstrate how complicated the conversion can be. You can use ISO8601DateFormatter if you use that format. Normally, the way we send date data is in ISO 8601 format. In the above example, a different time zone leads to a different date string, so you need to be explicit about this. You must be as explicit as you can to get the result you want. This case is as complicated as convert string to data. Setting a different timezone may result in a wrong date, and. ![]() timeZone = TimeZone (secondsFromGMT : 60 * 60 * 8 ) // 1 In this example, we convert a date to dd/MM/yyyy format. One example is when you try to send date data back to your backend. You do this kind of conversion when you have a specific string format in mind. Let's see two examples of data conversion.Ĭonvert to localized user-facing string. It can be complicated or easy based on the purpose of the result string. Data to StringĬonverting data to a string is another story. It can't be any easier, and this string to data conversion is not what the new API wants to improve. This kind of conversion is complicated since it includes a lot of moving parts. ![]() Formatter will use device locale and time zone, which may vary based on devices. If that information isn't in the string, we have to put it in the formatter class to be able to do the conversion. A time zone, in this case, is GMT+7 (Bangkok timezone)Īs you can see, there are some configurations (time zone and calendar) that we need to know before we can convert any string to a date. We specified a format of passing string. timeZone = TimeZone (secondsFromGMT : 60 * 60 * 7 ) // 2įormatter. let formatter = DateFormatter ( )įormatter. To convert this string, we create DateFormatter with the following properties. We will try to convert the following string into a date. For example, DateFormatter can convert string to date and date to string. They can convert data to string and vice-versa. Most formatters we have right now are not just simple formatter but converters. What the new formatter can do is a subset of what the old formatter can. The new formatter API is all about taking data, like numbers, dates, times, and more and converting it into a localized user-presentable string. What about old APIīefore digging into the new formatter API, I want to assure you that the old API doesn't go anywhere. Sponsor and reach thousands of iOS developers.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |