Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change string to date without converting to any timezone in Swift?

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?

like image 321
AAA Avatar asked Aug 19 '15 03:08

AAA


People also ask

How to convert string to date in Swift?

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

How do I format the current date in Swift?

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.

How do I get only time in Swift?

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


1 Answers

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"
}
like image 188
Leo Dabus Avatar answered Oct 01 '22 21:10

Leo Dabus