1

This is not working for me. I have a value I'm getting from a database that's clearly a valid Date and I can print it out fine:

let datefromu = "\(PFUser.current()!.object(forKey:"original_date_route_started")!)"
print("I fetched \(datefromu)")

that prints: I fetched 2017-08-10 15:50:43 +0000.

I must add that if I look in the raw database it is written as so: 2017-08-10T15:50:43.436Z

so I think that output is already Parse's way of formatting the output from the date field?

Bottom line, when I want to populate a date variable I have (original_date_route_started: Date?) and convert that string to a date, I'm getting nil!:

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
self.original_date_route_started = formatter.date(from: datefromu)
10
  • It seems that datefromu is already a Date and not a String. Commented Aug 10, 2017 at 16:50
  • 1
    Are you sure you didn't want to write formatter.string(from: datefromu)? Commented Aug 10, 2017 at 16:54
  • @rmaddy you are right. But how can I set it to my date variable? Swift doesn't know its a date. Commented Aug 10, 2017 at 16:56
  • @DávidPásztor no. I need a date not a string. And I already tried converting to string and back today again but to no avail. same issue Commented Aug 10, 2017 at 16:58
  • 1
    Use subclassing to avoid manual conversion of data types in Parse. If you have already set original_date_route_started's type as a Date in the Schema (using the dashboard or the REST API), it will be converted to Date (swift) / NSDate (objc). Commented Aug 10, 2017 at 17:01

1 Answer 1

1

Do you want to convert PFUser.current()!.object(forKey:"original_date_route_started") to a Date ? Then you should consider subclassing to avoid dealing with DateFormatters:

Assuming original_date_route_started is correctly defined as a Date in Parse (if not, I'll update the answer):

import Foundation
import Parse

class UserParse: PFUser {

    @NSManaged var original_date_route_started: Date?

}

If you want to set a new value to it:

var user = UserParse.current()!
user.original_date_route_started = Date(timeIntervalSinceNow: -3600 * 4) // 4hs ago
user.saveInBackground()

// Your sample code:
let datefromu = UserParse.current()!.original_date_route_started
self.original_date_route_started = datefromu

Are you just having problems setting self.original_date_route_started's value in another class that's not PFUser ? There are 2 ways to do it:

  • ISO8601DateFormatter for iOS >= 10 (See source code for more details)

    let isoDateFormatter= ISO8601DateFormatter()
    
  • Custom DateFormatter for iOS >= 2

    var isoDateFormatter: DateFormatter = {
        $0.locale     = Locale(identifier:"en_US_POSIX")
        $0.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
        $0.timeZone   = TimeZone(secondsFromGMT: 0) // Or Locale(identifier: "GMT")
        return $0
    }(DateFormatter())
    

Converting string to date:

let testString = "2017-08-10T17:53:51+0000"
isoDateFormatter.date(from: testString)
Sign up to request clarification or add additional context in comments.

4 Comments

I'm using original_date_route_started as a date value in many areas in my class, not necessarily within the scope of the Parse user. So I have it way up on top and keep referring to it as self.original_date_route_started It is only when I don't have value for it , then I retrieve it from the Parse User in db, and am currently unsuccessfully trying to set it from that value
yes and in your example, can you then set a variable that should be of type Date as so: self.original_date_route_started = isoDateFormatter.date(from: testString) I don't think so! - this is all I'm trying to do!
What do you mean with I don't think so! ? That's the API: func date(from string: String) -> Date?. Your DateFormatter was set up incorrectly, please use the code above (you forgot to setup the Locale / TZ and the format string is incorrect). Working example: swift.sandbox.bluemix.net/#/repl/598ca26ade80147f1a9d22f0
Thanks for the sample. let me check that out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.