简体   繁体   中英

NSDateformatter does not provide correct date swift

When I am using NSDateformatter to get date from string it returns wrong value.I dont know what must be reason.Please help me to resolve. Thanks in advance.

Input String:-

    newDate "06:11"
    end time "07:00"
    start time "05:00"

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "HH:mm"
    let date:NSDate = dateFormatter.dateFromString(dateString)!
    println(date)

Output Date:- 
current date 2000-01-01 00:41:00 +0000
end date 2000-01-01 01:30:00 +0000
start date 1999-12-31 23:30:00 +0000

By doing

println(date)

You are printing the description of the date, which is equal to -

println(date.description())

And it always prints the time in UTC (GMT) So if you are in UTC+5:30, it will display time that is before 5:30 hours than your entered time. If you want to print date in your timezone, use a NSDateFormatter again. To see date of other time zone, set NSDateFormatter timeZone.

Hope this explains.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM