Format String to HH:mm in Swift - ios

My code:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = dateFormatter.dateFromString("2015-09-01 00-32-40")
Result: 2015-08-31 17:32:40
But I want get result like this: 17:32. How do I resolve it?

If you're trying to get an NSDate and not a string representation, printing the NSDate will always show the full date time. To just show the hours and minutes, you'll have to create a string representation using an "HH:mm" date format, ex:
// Your original code
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = dateFormatter.dateFromString("2015-09-01 00-32-40")
// To convert the date into an HH:mm format
dateFormatter.dateFormat = "HH:mm"
let dateString = dateFormatter.stringFromDate(date!)
println(dateString)
Not sure why you're looking to get a result of "17:32", but this will produce "00:32" as a result from the original "00-32" portion of the string.

Related

How to format a date using timezone in Swift?

I have checked other questions but none of them helped me much.
I have following string:
let dateString = "2018-04-29T21:00:00.000Z"
I have successfully converted it to date using the following:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let convertedDate = dateFormatter.date(from: dateString)
But now I only want the time "hh:mm a" using timezone such as "+8". I have tried following way but it's not working:
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "UTC+8")
dateFormatter.dateFormat = "hh:mm a"
let requiredTime = dateFormatter.string(from: convertedDate!)
Can anyone help me to overcome this problem?
The format you want is hh:mm a Z, which will provide the +0800.
You want to create a TimeZone which +8 hours from GMT, normally I prefer to use the appropriate abrivations (ie AET), but I guess if you don't have that, you can create a TimeZone using secondsFromGMT, for example...
let tz = TimeZone(secondsFromGMT: 8 * 60 * 60)
let toFormatter = DateFormatter()
toFormatter.timeZone = tz
toFormatter.dateFormat = "hh:mm a Z"
let requiredTime = toFormatter.string(from: convertedDate!)
Which based on your example data, will produce a value of...
05:00 AM +0800

Converting UTC to EST in Swift

I have a string of text in my application that conveys a time and the date, such as let time = "2017-07-09T09:17:08+00:00". I want to take this string, which is in UTC, and convert it to a string that presents the time in EST. For my example, the resulting string would be 2017-07-09T05:17:08+00:00. How can this be done?
I've tried using
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatter.timeZone = TimeZone.current
let date = dateFormatter.date(from:time!)!
but printing the date gives me the same time in UTC.
All you need to do is to use a DateFormatter to convert your Date object into a String. By default, the date will be formatted to local time.
// First, get a Date from the String
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from:time!)!
// Now, get a new string from the Date in the proper format for the user's locale
dateFormatter.dateFormat = nil
dateFormatter.dateStyle = .long // set as desired
dateFormatter.timeStyle = .medium // set as desired
let local = dateFormatter.string(from: date)
Note that there is no need to set any timezone for either set of code in this case.

NSDate from string is always nil

I receive dates as String like this one:
"2016-05-20T12:25:00.0"
I want to get its corresponding NSDate object, and I'm trying this way:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "YYYY-MM-DDThh:mm:ss.s"
let date = dateFormatter.dateFromString(dateStr)
where dateStr is like the example I wrote first. I took the dateFormat string from this page, but I get a nil date, what is wrong there?
Thanks in advance
You have a few problems with your date format. First Y is for weekOfYear, D is for day of year. hh is used for 12 hours format, decimal second you should use capital S and you need to escape the 'T'
You should do as follow:
let dateString = "2016-05-20T12:25:00.0"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.S"
if let date = dateFormatter.dateFromString(dateString) {
print(dateFormatter.stringFromDate(date) ) // "2016-05-20T12:25:00.0\n"
}
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.S"
if let date = dateFormatter.stringFromDate(NSDate()) {
print(dateFormatter.stringFromDate(date) )
}

Convert Optional string to date

I have this Optional(2015-07-27 19:29:50 +0000) as a string? and I want to convert it to a date (NSDate) but I can't figure it out I've been searching a lot for solutions but I am pretty new to coding so if anyone could help I would appreciate it.
This is what I am using to convert the string to a date currently.
note: dateString is Optional(2015-07-27 19:29:50 +0000)
dateFormatter = NSDateFormatter()
println(dateFormatter)
dateFormatter.dateFormat = "yyyy-MM-dd"
let s = dateFormatter.dateFromString(dateString)
I always get a nil value
If you are getting nil from NSDateFormatter, you likely specified incorrect date format. Try this:
let strTime = "2015-07-27 19:29:50 +0000"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
formatter.dateFromString(strTime) // Returns "Jul 27, 2015, 12:29 PM" PST
You will want to use a NSDateFormatter and call dateFromString: on it to get the date from the string. See Apple's NSDateFormatter documentation for a great explanation of doing what you want.
In swift 3.0:
let strTime = "2015-07-27 19:29:50 +0000"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
formatter.date(from: strTime)
dateString is already date and need to convert to string. Below code is working fine
dateFormatter = NSDateFormatter()
println(dateFormatter)
dateFormatter.dateFormat = "yyyy-MM-dd"
let s = dateFormatter.string(from: dateString)

Swift make NSDate from string not working as expected

I'm trying to convert a date string into a NSDate. I have this input date string 2014-10-09 17:57 and this timezone +4 or Asia/Dubai and for some reason, i get this NSDate after i execute the code below 2014-10-09 13:57:00 +0000.
let dateString = "2014-10-09 17:57"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm"
formatter.timeZone = NSTimeZone(name: "Asia/Dubai")
println("\(formatter.dateFromString(dateString))")
I want to get a date like this 2014-10-09 17:57 +0400. What am i doing wrong? Can you help me figure it out? I need the date to make some calculations between this one and another one which is correctly formatted with timezone.
It helps to breakup compound statements. The result of the formatter is a date, seconds since the reference date (GMT), it has no concept of a format or timezone.
The println() displays the date based ion the default formatting of there description method.
If you want it display in a particular way use a date formatter to create the desired string representation.
Example:
let dateString = "2014-10-09 17:57"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm"
formatter.timeZone = NSTimeZone(name: "Asia/Dubai")
let date = formatter.dateFromString(dateString)
println("date: \(date!)") // date: 2014-10-09 13:57:00 +0000
formatter.dateFormat = "yyyy-MM-dd HH:mm Z"
let formattedDateString = formatter.stringFromDate(date!)
println("formattedDateString: \(formattedDateString)") // formattedDateString: 2014-10-09 17:57 +0400

Resources