首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >iOS Swift将日历组件int月份转换为中等样式的字符串月份

iOS Swift将日历组件int月份转换为中等样式的字符串月份
EN

Stack Overflow用户
提问于 2015-06-12 06:06:10
回答 6查看 23.3K关注 0票数 24

我想以这种格式显示日历

给用户。一种选择是使用"string range“来获取各个日历组件。第二种方法是使用NSCalendar获取它,这在我看来是更好的(是吗?)。所以我的代码如下所示。但有两个问题。

  1. I‘m’t Int the local time form "hour & minute“
  2. I‘m Int in Int. (我没有得到当地时间”小时和分钟组件“)。我希望它是字符串(mediumStyle中的月份)

有人知道怎么弄到我需要的东西吗?所附图片正是我想要达到的目标。在这里,我使用了三个UILabel,一个表示“日期”,第二个表示“月、年”,第三个表示“时间”。

任何帮助都将不胜感激。

代码语言:javascript
复制
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)
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30792264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档