Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Swift converting calendar component int month to medium style string month

Tags:

I want to display calendar in this format

visible calendar style date

to the user. One option is to use "string range" to get the individual calendar components. The second one is to get it using NSCalendar which to me looks like the better one (is it?). So my code is as below. But there are two problems.

  1. I am not getting the local time form "hour & minute components"
  2. I am getting month in Int. I want it to be in String (month in mediumStyle)

Anyone know how to get what I need? Image attached is what exactly I want to achieve. There I am using three UILabel one for "date", second for "month, year" and third for "time".

Any help would be appreciated.

var inputDateString = "Jun/12/2015 02:05 Am +05:00"  override func viewDidLoad() {     super.viewDidLoad()     let newDate = dateformatterDateString(inputDateString)     let calendar = NSCalendar.currentCalendar()     let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitMonth | .CalendarUnitYear | .CalendarUnitDay, fromDate: newDate!)      let hour = components.hour     let minutes = components.minute     let month = components.month     let year = components.year     let day = components.day      println(newDate)     println(components)     println(day)     // 12     println(month)   // 6 -----> Want to have "Jun" here     println(year)    // 2015     println(hour)    // 2 ------> Want to have the hour in the inputString i.e. 02     println(minutes) // 35 ------> Want to have the minute in the inputString i.e.  05 }  func dateformatterDateString(dateString: String) -> NSDate? {     let dateFormatter: NSDateFormatter = NSDateFormatter()     dateFormatter.dateFormat = "MMM/dd/yyyy hh:mm a Z"     //      dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")     dateFormatter.timeZone = NSTimeZone.localTimeZone()     return dateFormatter.dateFromString(dateString) } 
like image 917
NS1518 Avatar asked Jun 11 '15 22:06

NS1518


1 Answers

You can use DateFormatter as follow:

extension Formatter {     static let monthMedium: DateFormatter = {         let formatter = DateFormatter()         formatter.dateFormat = "LLL"         return formatter     }()     static let hour12: DateFormatter = {         let formatter = DateFormatter()         formatter.dateFormat = "h"         return formatter     }()     static let minute0x: DateFormatter = {         let formatter = DateFormatter()         formatter.dateFormat = "mm"         return formatter     }()     static let amPM: DateFormatter = {         let formatter = DateFormatter()         formatter.dateFormat = "a"         return formatter     }() } extension Date {     var monthMedium: String  { return Formatter.monthMedium.string(from: self) }     var hour12:  String      { return Formatter.hour12.string(from: self) }     var minute0x: String     { return Formatter.minute0x.string(from: self) }     var amPM: String         { return Formatter.amPM.string(from: self) } } 

let date = Date()  let dateMonth  = date.monthMedium  // "May" let dateHour   = date.hour12       // "1" let dateMinute = date.minute0x     // "18" let dateAmPm = date.amPM           // "PM" 
like image 115
Leo Dabus Avatar answered Nov 11 '22 17:11

Leo Dabus