I know there are lot of questions passing around over this simple issue, but still I couldn't get a clear idea.
Here is what I want:
SelectedDateString = "19-08-2015 09:00 AM"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm a"
dateFormatter.timeZone = NSTimeZone();
let SelectedUTCDate = dateFormatter.dateFromString(SelectedDateString)!
println("SelectedLocalDate = \(SelectedLocalDate)")
// OUTPUT: SelectedLocalDate = 2015-08-18 18:30:00 +0000
If I dont use TimeZone:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm a"
// dateFormatter.timeZone = NSTimeZone();
let SelectedUTCDate = dateFormatter.dateFromString(SelectedDateString)!
println("SelectedLocalDate = \(SelectedLocalDate)")
//OUTPUT: SelectedLocalDate = 2015-08-18 19:26:00 +0000
Why is there change in the time and Date? What I want is:
//OUTPUT: SelectedLocalDate = 2015-08-19 09:00:00 +0000
Also I want to convert the Local Date to Exact UTC Date
//Like this: SelectedUTCDate = 2015-08-19 03:30:00
inputString = "19-08-2015 10:45 am" // My localtime is 10:35 AM, so I set 10 mins from now
var currentUTCTime = NSDate() // currentUTCTime is 05: 15 AM.
I want to convert the inputString to its respective UTC Time and find the difference between the two times in both date and string.
//Like this Date: diffInDate: 00-00-0000 00:10 and
// Like this String: diffInString: 10 mins
How can I get both of these?
dateFormat = "yyyy-MM-dd'T'HH:mm:ss" let oldDate = olDateFormatter. date(from: inputDate) let convertDateFormatter = DateFormatter() convertDateFormatter. dateFormat = "MMM dd yyyy h:mm a" return convertDateFormatter. string(from: oldDate!) }
Get current time in “YYYY-MM–DD HH:MM:SS +TIMEZONE” format in Swift. This is the easiest way to show the current date-time.
To get only current time, containing hour, minute and second, use Date and DateFormatter . Date returns current point in time, while DateFormatter with specified dateFormat formats this Date value to required format, like in this case returning only time (hour, minute and second).
let dateString = "19-08-2015 09:00 AM"
let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(identifier: .iso8601)
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm a"
if let dateFromString = dateFormatter.date(from: dateString) {
print(dateFromString) // "2015-08-19 09:00:00 +0000"
dateFormatter.timeZone = .current
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm a Z"
dateFormatter.string(from: dateFromString) // 19-08-2015 06:00 AM -0300"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With