Questions tagged [dateformatter]

DateFormatter is a type for converting between native date objects and string representation of those dates

DateFormatter is a type for converting between native date objects and string representation of those dates.

Date formatters exist in different frameworks and libraries:

340 questions
71
votes
2 answers

Show AM/PM in capitals in swift

I wrote the following code to show a datetime in a particular format: let formatter = NSDateFormatter() formatter.dateStyle = NSDateFormatterStyle.LongStyle formatter.timeStyle = .MediumStyle formatter.dateFormat = "HH:mm a 'on' MMMM dd, yyyy" let…
Amit Raj
  • 1,358
  • 3
  • 22
  • 47
66
votes
4 answers

How to convert a date string with optional fractional seconds using Codable in Swift?

I am replacing my old JSON parsing code with Swift's Codable and am running into a bit of a snag. I guess it isn't as much a Codable question as it is a DateFormatter question. Start with a struct struct JustADate: Codable { var date: Date …
Guillermo Alvarez
  • 1,695
  • 2
  • 18
  • 23
44
votes
8 answers

How do I get the current Date in short format in Swift

In the images below you can see the code I wrote and the values of all the variables: class fun getCurrentShortDate() -> String { var todaysDate = NSDate() var dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "dd-MM-yyyy" …
Friso Buurman
  • 569
  • 1
  • 4
  • 7
25
votes
6 answers

How to parse date-time with two or three milliseconds digits in java?

Here is my method to parse String into LocalDateTime. public static String formatDate(final String date) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SS"); LocalDateTime formatDateTime =…
brain storm
  • 30,124
  • 69
  • 225
  • 393
13
votes
1 answer

How can you properly support the iOS 12 hour / 24 hour time override in Date & Time settings for DateFormatters?

If your iOS device is set to use a region that defaults to 12 hour time (e.g. United States) and you have a DateFormatter set up like this: var dateFormatter: DateFormatter = { let formatter = DateFormatter() formatter.dateFormat = "h:mm a" …
shim
  • 9,289
  • 12
  • 69
  • 108
11
votes
2 answers

Date format in Swift TODAY TOMORROW YESTERDAY

I want to show a date as Saturday June 13. If the date is current day it should display Today like that Tomorrow, Yesterday. I couldn't achieve both guard let date = Date(fromString: "16 September 2020", format: "dd MMMM yyyy")…
Anees
  • 514
  • 5
  • 20
11
votes
1 answer

What are the format specifiers allowed in iOS DateFormatter dateFormat fromTemplate?

I cannot find a list of the format specifiers allowed in the template argument of DateFormatter.dateFormat(fromTemplate:options:locale:). dateFormat directs me to... Date and Time Programming Guide. None of those pages lists the format specifiers.…
Bob Peterson
  • 636
  • 7
  • 16
9
votes
0 answers

Date format "dd-MMM-yyyy" for September now Sept. Not Sep so converting causes nil

Updated iPad to iOS 15.4. Now String to Date conversion using DateFormatter returns nil when stored month format "MMM" = Sep. Works ok when MMM Month = Sept. Did work in iOS 15.3 and earlier. Issue is that I have dates created and stored as…
GDev
  • 91
  • 4
7
votes
2 answers

Swift DateFormatter returning wrong date

I have a issue with getting the correct date in the following case let dateString = "May 2, 2018 at 3:31 PM" let dateFormatter = DateFormatter() dateFormatter.dateFormat = "MMM dd, yyyy 'at' HH:mm a" dateFormatter.timeZone = TimeZone(identifier:…
AndQ
  • 103
  • 1
  • 6
7
votes
3 answers

How to remove 'at' from formatted date string?

This is the date formatter setup: let dateFormatter = DateFormatter() dateFormatter.dateStyle = .medium dateFormatter.timeStyle = .medium Expected output: Aug 13, 2017 6:04:11 PM Current output: Aug 13, 2017 at 6:04:11 PM How can the 'at' word be…
Andras Hatvani
  • 4,346
  • 4
  • 29
  • 45
6
votes
1 answer

iOS Date Formatting without year, with correct localization

Do you know how I could have the following date string Jun 28 on iOS? Jun 28 in English 28 juin in French etc I want basically short month, day, without year. (in the localized order, so day, month in French for instance) I tried the…
papay0
  • 1,311
  • 1
  • 12
  • 25
6
votes
1 answer

iOS Swift how to have DateFormatter without year for any locale?

I want to create a date string from two dates in format like : "Apr 21 - Sep 17, 2018" - US "21 Apr - 17 Sep, 2018" - Elsewhere While respecting local date formats (MMM dd for US, dd MMM for Europe, etc). How do I get the DateFormatter to give me…
Alex Stone
  • 46,408
  • 55
  • 231
  • 407
5
votes
1 answer

DateTimeFormatter exception parsing its own print result

I don't understand something, I have this code : DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"); System.out.println(LocalDateTime.parse(LocalDateTime.now().format(fmt), fmt)); This generate and exception : Exception in…
iXô
  • 1,133
  • 1
  • 16
  • 39
5
votes
3 answers

Handling multiple formats in DateTimeFormatter

I am developing using Java 8 a function that must handle the conversion from String to LocalDateTime of the following dates: 2019-06-20 12:18:07.207 +0000 UTC 2019-06-20 12:18:07.20 +0000 UTC 2019-06-20 12:18:07.2 +0000 UTC 2019-06-20 12:18:07…
riccardo.cardin
  • 7,971
  • 5
  • 57
  • 106
5
votes
3 answers

How to parse offset with colon using DateTimeFormatter?

I have the following string: String timeStamp = "2020-01-31 12:13:14 +03:00". And I tried to parse it using Java 8 DateTimeFormatter. DateTimeFormatter formatter = DateTimeFormatter.ofPattern( format ); tmpTimestamp = ZonedDateTime.parse( timeStamp,…
1
2 3
22 23