Date From Timestamp Becomes Wrong [duplicate] - ios

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))

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
}

Wrong behavior with pt-BR DateFormatter Swift [duplicate]

This question already has answers here:
1st april dates of 80s failed to parse in iOS 10.0
(1 answer)
Why NSDateFormatter is returning null for a 19/10/2014 in a Brazilian time zone?
(3 answers)
DateFormatter's returns nil for specific date strings without time in Swift
(1 answer)
Closed 3 years ago.
I have this method that parse a string Date:
let dateFormatterGet = DateFormatter()
let stringDate = "31/01/1965"
dateFormatterGet.dateFormat = "dd-MM-yyyy"
dateFormatterGet.locale = Locale(identifier: "pt_BR")
let birthDate = dateFormatterGet.date(from: stringDate)
print(birthDate)
If Date is 31/01/1965 (January 31st), the birthDate is returning nil, but if i change the day for 30, all works fine. What is wrong here?

Convert from 2018-10-23T06:01:10.806Z to 10-May-2018 and a seperate string for time [duplicate]

This question already has answers here:
How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift?
(13 answers)
Closed 4 years ago.
have to extract a separate date and time from the same string i.e)2018-10-23T06:01:10.806Z, the date must be in the format of 10-May-2018 and the time must be in 12 hours format i.e) 08:00 PM
Try this
func formatDateString(dateString: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
return dateFormatter.date(from: dateString)
}
You get the Date object, do whatever you can do whatever you wanted using that.

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 TimeString yyyy-mm-dd HH:mm:ss to NSDate Time HH:mm AM/PM in swift [duplicate]

This question already has answers here:
How can I convert string date to NSDate?
(18 answers)
Closed 6 years ago.
I want to convert Time string to NSDate Time into HH:mm: a format. I have a code right now
let defaultStartTime = json!["start_schedule_datetime"] as? String
let defaultEndTime = json!["end_schedule_datetime"] as? String
print(defaultStartTime,defaultEndTime)
dispatch_async(dispatch_get_main_queue(), {
let dtf = NSDateFormatter()
dtf.timeZone = NSTimeZone(name: "UTC")
dtf.dateFormat = "HH:mm a"
let defaultStartdate : NSDate = dtf.dateFromString(defaultStartTime!)!
let defaultEnddate : NSDate = dtf.dateFromString(defaultEndTime!)!
print(defaultStartdate,defaultEnddate)
self.startTime.date = defaultStartdate
self.endTime.date = defaultEnddate
self.endTime.reloadInputViews()
self.startTime.reloadInputViews()
})
Where:-
start_schedule_datetime = 2016-05-27 13:35:00
end_schedule_datetime = 2016-05-27 19:35:00
Here i want time into HH:mm a. But when i am trying to do that i am getting
fatal error: unexpectedly found nil while unwrapping an Optional value
Can any one please help me here. Thank you
Here one of the best solution for everyone.
You can easily get what you want from the date. And also you can learn about dateformatter.
Reference 1: http://nsdateformatter.com/
EDIT 1
Reference 2: For Ready made code
Good luck.
Check Below Answer. Both Current date and Date picker date is displaying.
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
print(dateFormatter.stringFromDate(NSDate())) //if you want current date
print(dateFormatter.stringFromDate(yourdatepickername.date) //if you want your selective date

Resources