DateFormatter returns wrong time [duplicate] - ios

This question already has answers here:
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 5 years ago.
I did an extension for Date that returns a formatted string:
extension Date {
var myFormattedDate : String {
let formatter = DateFormatter()
formatter.timeZone = TimeZone.current
formatter.dateFormat = "EEEE, MMMM d, y (HH:mm a)"
return formatter.string(for: self)!
}
}
On runtime, I set a breakpoint inside the myFormattedDate property.
po self printed:
2017-09-05 08:50:00 +0000
po formatter.string(for: self)! printed:
Tuesday, September 5, 2017 (11:50 AM)"
What could be the problem?
Thanks!

Printing a Date always returns an UTC time, regardless of the local time zone. Just avoid printing a Date object directly if you want to see the date with the proper time zone in your console.

Related

What is the correct date format for this date-as-a-string? [duplicate]

This question already has answers here:
How to convert "2017-07-11T06:52:15.948Z" in to like "JUL, 7 2017" in swift
(1 answer)
How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift?
(13 answers)
Closed 1 year ago.
The back end is providing me with this date-as-a-string: 2021-09-10T12:57:01.671Z
I need to convert that string to a Date using the iOS DateFormatter; and to do so I need to set the formatter's dateFormat property.
I have tried all sorts of combinations, with no luck. Specifically I am struggling with the .671Z part.
What is the correct date format to use?
You need "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" as date format
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
guard let date = dateFormatter.date(from: self) else {
// fallback if date is not in correct format
return nil
}

iOS DateFormatter giving nil ,When device Timezone and region changes [duplicate]

This question already has answers here:
What is the best way to deal with the NSDateFormatter locale "feature"?
(4 answers)
DateFormatter doesn't return date for "HH:mm:ss"
(1 answer)
Swift: NSDateFormatter dateFromString returns nil on devices
(1 answer)
Closed 4 years ago.
I have a date in string like this
let time = "Wed Oct 10 12:22:32 UTC 2018"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EE MM dd HH:mm:ss zz yyyy"
let rawDate = dateFormatter.date(from: time) // GETTING rawDate
dateFormatter.dateFormat = "d MMM yyyy, h:mm a"
guard let date = rawDate else { return "" } // rawDate NIL HERE
let time = dateFormatter.string(from: date)
It works fine in my timezone Mumbai and region India. But when i'm changing it to some other region and timezone, rawDate is getting nil.
What's wrong i'm doing?
P.S: Please give a generic answer, No fixed timezone or Locale is needed.
Update: As the duplicate reference consist en_US posfix, This doesn't make code generic, if I use en_GB or something else the data will be incorrect.

Date From Timestamp Becomes Wrong [duplicate]

This question already has answers here:
NSDate timeIntervalSince1970 not working in Swift? [duplicate]
(3 answers)
Closed 4 years ago.
I am trying to convert timestamp to Date. But it returns wrong date.
I have a time stamp
1524637838000.0
Which returns 25-04-2018 12:00 as per this online converter
But I get wrong date when convert using my code
let date = Date(timeIntervalSince1970:1524637838000.0)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm"
let myDate = dateFormatter.string(from: date)
print("Date is = ",myDate)
I get
07-11-50283 12:03
as result. Is there anything wrong with my code?
your timestamp 1524637838000.0 in milliseconds, it should be in seconds, so your date initialization should be:
let date = Date(timeIntervalSince1970:(1524637838000.0/1000))

How to convert milliseconds to date string in swift 3 [duplicate]

This question already has answers here:
NSDate timeIntervalSince1970 not working in Swift? [duplicate]
(3 answers)
Closed 6 years ago.
I am trying to convert milliseconds to date string in swift 3,i tried by setting date fomatter but i am not getting current date string.
var milliseconds=1477593000000
let date = NSDate(timeIntervalSince1970: TimeInterval(milliseconds))
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
formatter.locale = NSLocale(localeIdentifier: "en_US") as Locale!
print(formatter.string(from: date as Date))
output:
22-01-48793 01:30:00
Try this,
var date = Date(timeIntervalSince1970: (1477593000000 / 1000.0))
print("date - \(date)")
You will get output as date :
date - 2016-10-27 18:30:00 +0000
How about trying this -
let milisecond = 1479714427
let dateVar = Date.init(timeIntervalSinceNow: TimeInterval(milisecond)/1000)
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm"
print(dateFormatter.string(from: dateVar))
Have a look at the documentation of NSDate:
convenience init(timeIntervalSince1970 secs: TimeInterval)
Returns an NSDate object initialized relative to the current date and time by a given number of seconds.
Just convert your milliseconds to seconds and you should get the correct date.

Convert String to Date Returns nil for afternoon times only [duplicate]

This question already has answers here:
NSDateFormatter producing (null) SQLite and iOS 5.1
(2 answers)
Closed 6 years ago.
I have the code to convert a string to a date as:
let dateString = detailData.value["eventStart"] as! String
print(dateString)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss Z" /*find out and place date format from http://userguide.icu-project.org/formatparse/datetime*/
let date = dateFormatter.dateFromString(dateString) // || date!.timeIntervalSince1970 >= presentDate.timeIntervalSince1970
print(date?.timeIntervalSince1970)
The output is:
2016-05-31 03:18:11 +0000
Optional(1464664691.0)
2016-06-03 14:00:44 +0000
nil
2016-06-01 00:38:08 +0000
Optional(1464741488.0)
2016-06-04 00:25:55 +0000
Optional(1464999955.0)
2016-05-31 22:19:54 +0000
nil
I see that the time in the afternoon (is 12+) return nil.
How can I prevent this?
Your date parsing is failing. In your case, its failing because of an invalid hour component in the input string.
Use 24-hour format HH instead of 12-hour format hh.

Resources