0

I want a date as 07:05 or 16:25

I tried but it does not work exactly

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)

the result:

[, 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]

The dates do not have the 0 before it

example

8:3 -> I want 08:03 
7:0 -> I want 07:00  
10:5-> I want 10:05
maRtin
  • 6,336
  • 11
  • 43
  • 66

3 Answers3

0

As the other poster said, why not use your already created date formatter to create the output strings?

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

If you want to do math on your dates, you are better off converting to NSDate, then using NSCalendar methods like dateByAddingComponents:toDate:options:

That method would let you add hours, minutes, or whatever to your dates and handle all the overflows correctly. Then just output your resulting date using your date formatter's dateFromString method as above.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

If you are determined to generate your output strings yourself, you can use the stringWithFormat intializer for String:

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

But it is really much better to use NSCalendar to add component values to your dates and then use your date formatter to output them.

The date calculations using NSCalendar will handle the dozens of edge cases correctly (roll-over of hour, roll-over to next day, roll-over to next year, leap years, different calendars, etc, etc.)

The date formatter will handle localization of your output format much more gracefully than you will, especially if you use styles rather than explicit date formats.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

just need to comp.minute += 10.

the detail:

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)
nathanwhy
  • 5,884
  • 1
  • 12
  • 13