Robin JR
Robin JR

Reputation: 141

Date format Convertion issue in swift

i could not convert string date into NSDate object. Please check below code

let stringDate = "06:30 AM" 
let formatter = NSDateFormatter()
formatter.dateFormat="hh:mm a"
let local = NSLocale(localeIdentifier: "en_US")
formatter.locale=local
let date = formatter.dateFromString(stringDate)

Upvotes: 0

Views: 509

Answers (1)

dfrib
dfrib

Reputation: 73186

The output is as expected, and depending on what you're trying to achieve, you haven't really done anything wrong.

Your stringDate instance contains only information about a time of the day, not a date (the prior is also the only format your NSDateFormatter formatter is "interested" in). Hence, the following snippet produces the expected 06:30 AM output:

let stringDate = "06:30 AM"
let formatter = NSDateFormatter()
formatter.dateFormat="hh:mm a"
let local = NSLocale(localeIdentifier: "en_US")
formatter.locale=local

if let date = formatter.dateFromString(stringDate) {
    print(formatter.stringFromDate(date)) // 06:30 AM
}

NSDate instances are defined, however, as single point in time (date and hour of the day), with reference to an absolute reference date:

NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001).

From the language reference for NSDate.

Hence, in addition to a time of day, NSDate instances include also a date (even if this is not, in your case, used or displayed). When you assign a value to date above, the Swift playground displays the time of day of the correct date; the latter offset by 06:30 from the absolute reference date, 2000-01-01 00:00:00. If we modify the example above to print all details in the final print statement, we see this more clearly:

// ...

if let date = formatter.dateFromString(stringDate) {
    formatter.dateStyle = .FullStyle
    formatter.timeStyle = .FullStyle
    print(formatter.stringFromDate(date))
    /* Saturday, January 1, 2000 at 6:30:00 AM Central European Standard Time */
}

(Addition with regard to your comments below)

Note the difference of printing the date object itself (e.g. print(date) above) and printing a ** formatted string representation** of the date using your formatter (e.g. print(formatter.stringFromDate(date))). The prior just prints the .description property of your date, which is an default-formatted string representation of the contents of object itself rather than a controlled formatted output of the date:

Declaration

var description: String { get }

Description

A string representation of the date object. (read-only)

The representation is useful for debugging only.

There are a number of options to acquire a formatted string for a date including: date formatters (see NSDateFormatter and Data Formatting Guide), and the NSDate methods descriptionWithLocale:, dateWithCalendarFormat:timeZone:, and descriptionWithCalendarFormat:timeZone:locale:

Refer to my code blocks above to see how you can print the formatted date using your NSFormatter.

Upvotes: 1

Related Questions