Swift DateTime Formatter returning nil with matching format

Mark McGookin :

When parsing an input datetime string with the matching format, I am still getting nil back from DateFormatter in swift.

Input string (copied at runtime from variable) and confirmed that is the string running the call with Postman.

"2020-06-30T14:41:33.9"

Parsing method

static func FormatDateTimeString(dateTimeString: String) -> String {
    let inputDateFormatter = DateFormatter()
    inputDateFormatter.dateFormat = "YYYY-MM-DDThh:mm:ss.s"
    let date = inputDateFormatter.date(from:dateTimeString) //date is always nil
    
    let outputDateFormatter = DateFormatter()
    outputDateFormatter.dateFormat = "MMM d, yyyy HH:mm"
    
    return outputDateFormatter.string(from: date!)
}

I have also tried "YYYY-MM-DD'T'hh:mm:ss.s" and "YYYY-MM-DDThh:mm:ss" but none seem to work.

EDIT: Trying the format suggested below I am still getting nil.

enter image description here

EDIT: As Suggested from the comments I am still getting nil. Format below is: inputDateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.S"

enter image description here

EDIT: Still getting null using the following config

enter image description here

Mojtaba Hosseini :

Use this format:

"yyyy-MM-dd'T'HH:mm:ss.S"

Demo

let dateTimeString = "2020-06-30T14:41:33.9"

let inputDateFormatter = DateFormatter()
inputDateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.S"
let date = inputDateFormatter.date(from: dateTimeString) // "Jun 30, 2020 at 2:41 PM"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

  1. 1

    pump.io port in URL

  2. 2

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  5. 5

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  6. 6

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  7. 7

    mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '127.0.0.1:3306' (111 Connection refused)

  8. 8

    What's the point of declaring a const in JavaScript

  9. 9

    The prefix "andriod" for attribute "andriod:name" associated with an element type "application" is not bound?

  10. 10

    Change both state and params dynamically in ui-sref

  11. 11

    Copying a slide from one Google Slides presentation into another

  12. 12

    Grails with Oracle thick OCI driver authenticate to Oracle with wrong user

  13. 13

    Converting a class method to a property with a backing field

  14. 14

    How to use Angular2 and Typescript in Jsfiddle

  15. 15

    clojure.lang.LazySeq cannot be cast to class clojure.lang.Associative

  16. 16

    Inner Loop design for webscrapping

  17. 17

    How to set tab order for array of cluster,where cluster elements have different data types in LabVIEW?

  18. 18

    Removed zsh, but forgot to change shell back to bash, and now Ubuntu crashes (wsl)

  19. 19

    IServiceCollection does not contain a defintion for AddHttpClient

  20. 20

    What's the difference between conflict and compulsory cache miss?

  21. 21

    How to run blender on webserver?

HotTag

Archive