Xcode Swift NSDate 自定义

Xcode Swift NSDate Custom

我想要一个 07:05 或 16:25

的日期

我试过了,但效果不佳

var array1 = ["05:30","05:50","06:10","06:30","06:50","07:05","07:13","07:19","07:25","07:30","07:35","07:40","07:46","07:53","07:59","08:06","08:12","08:20","08:28","08:37","08:46","08:55","09:05","09:15","09:25","09:35","09:45","09:55","10:05","10:15","10:25","10:35","10:45","10:55","11:05","11:15","11:25","11:35","11:45","11:55","12:05"]

 var arrr  = [String()]
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "HH:mm"
        let calendar = NSCalendar.currentCalendar()


        for ora in array1 {



            var date = dateFormatter.dateFromString(ora)
             var comp = calendar.components((.CalendarUnitHour | .CalendarUnitMinute), fromDate: date!)
            var hour = comp.hour
            var minute = comp.minute

             minute = minute + 10

            if minute >= 60 {

                minute = minute - 60
                hour  = hour + 1

            }
            var nuovoOrario = "\(hour):\(minute)"
            arrr.append(nuovoOrario)

        }


        println(arrr)

结果:

[, 5:40, 6:0, 6:20, 6:40, 7:0, 7:15, 7:23, 7:29, 7:35, 7:40, 7:45, 7:50, 7:56, 8:3, 8:9, 8:16, 8:22, 8:30, 8:38, 8:47, 8:56, 9:5, 9:15, 9:25, 9:35, 9:45, 9:55, 10:5, 10:15, 10:25, 10:35, 10:45, 10:55, 1K1:5, 11:15, 11:25, 11:35, 11:45, 11:55, 12:5, 12:15, 5:40, 6:0, 6:20, 6:40, 7:0, 7:15, 7:23, 7:29, 7:35, 7:40, 7:45, 5:40, 6:0, 6:20, 6:40, 7:0, 7:15, 7:23, 7:29, 7:35, 7:40, 7:45, 5:40, 6:0, 6:20, 6:40, 7:0, 7:15, 7:23, 7:29, 7:35, 7:40, 7:45, 5:40, 6:0, 6:20, 6:40, 7:0, 7:15, 7:23, 7:29, 7:35, 7:40, 7:45, 5:40, 6:0, 6:20, 6:40, 7:0, 7:15, 7:23, 7:29, 7:35, 7:40, 7:45, 5:40, 6:0, 6:20, 6:40, 7:0, 7:15, 7:23, 7:29, 7:35, 7:40, 7:45]

日期前面没有0

示例

8:3 -> I want 08:03 
7:0 -> I want 07:00  
10:5-> I want 10:05

正如其他发帖人所说,为什么不使用您已经创建的日期格式化程序来创建输出字符串?

var array1 = 
  ["05:30","05:50","06:10","06:30","06:50","07:05","07:13","07:19",

  "07:25","07:30","07:35","07:40","07:46","07:53","07:59","08:06",
  "08:12","08:20","08:28","08:37","08:46","08:55","09:05","09:15",
  "09:25","09:35","09:45","09:55","10:05","10:15","10:25","10:35",
  "10:45","10:55","11:05","11:15","11:25","11:35","11:45","11:55",
  "12:05"]
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm"
for aString in array1
{
  if let aDate = dateFormatter.dateFromString(aString)
  {
    println("date = \(dateFormatter.stringFromDate(aDate))")
  }
}

如果你想对你的日期做数学运算,你最好转换成 NSDate,然后使用像 dateByAddingComponents:toDate:options:

这样的 NSCalendar 方法

该方法可以让您在日期中添加小时、分钟或其他任何内容,并正确处理所有溢出。然后只需使用日期格式化程序的 dateFromString 方法输出结果日期,如上所示。

如果您决定自己生成输出字符串,您可以使用字符串的 stringWithFormat 初始化程序:

let timeString = String(format: "%02d:%02d", hour, minute)

但使用 NSCalendar 将组件值添加到您的日期,然后使用您的日期格式化程序输出它们确实要好得多。

使用 NSCalendar 的日期计算将正确处理数十种边缘情况(小时翻转、翻转到第二天、翻转到下一年、闰年、不同日历等)

日期格式化程序将比您更优雅地处理输出格式的本地化,尤其是当您使用样式而不是明确的日期格式时。

只需comp.minute += 10.

详情:

var array1 = ["05:30","05:50","06:10","06:30","06:50","07:05","07:13","07:19","07:25","07:30","07:35","07:40","07:46","07:53","07:59","08:06","08:12","08:20","08:28","08:37","08:46","08:55","09:05","09:15","09:25","09:35","09:45","09:55","10:05","10:15","10:25","10:35","10:45","10:55","11:05","11:15","11:25","11:35","11:45","11:55","12:05"]

        var arrr  = [String()]
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "HH:mm"
        let calendar = NSCalendar.currentCalendar()


        for ora in array1 {

            var date = dateFormatter.dateFromString(ora)
            var comp = calendar.components((.CalendarUnitHour | .CalendarUnitMinute), fromDate: date!)

            comp.minute = comp.minute + 10

            var newDate = calendar.dateFromComponents(comp)

            var dateString = dateFormatter.stringFromDate(newDate!)

            arrr.append(dateString)

        }
        println(arrr)