Thank you everyone. I simplified the extension and now have it working
properly using a formatter.

extension NSDate
{
    convenience
    init(dateString:String) {
        let dateStringFormatter = NSDateFormatter()
        dateStringFormatter.calendar = NSCalendar(calendarIdentifier:
NSCalendarIdentifierGregorian)
        dateStringFormatter.dateFormat = "HH:mm"
        dateStringFormatter.locale = NSLocale(localeIdentifier:
"en_US_POSIX")
        dateStringFormatter.timeZone = NSTimeZone.localTimeZone()
        let d = dateStringFormatter.dateFromString(dateString)!
        self.init(timeInterval:0, sinceDate:d)
    }
}

...

let val = 10 //Test
let calendar = NSCalendar.currentCalendar()
calendar.timeZone = NSTimeZone.localTimeZone()
calendar.locale = NSLocale(localeIdentifier: "en_US_POSIX")
let startDate = NSDate(dateString:"11:30")
let date = calendar.dateByAddingUnit(.Minute, value: val, toDate:
startDate, options: [])

let formatter = NSDateFormatter()
formatter.dateStyle = .NoStyle
formatter.timeStyle = .ShortStyle

let dateString = formatter.stringFromDate(date!)
print(dateString) //*Correct*



On Tue, Dec 8, 2015 at 5:24 PM David Duncan <david.dun...@apple.com> wrote:

> On Dec 8, 2015, at 2:04 PM, Eric E. Dolecki <edole...@gmail.com> wrote:
>
> I tried an extension I found. It's off by 5 hours...
>
> extension NSDate
> {
>     convenience
>     init(dateString:String) {
>         let dateStringFormatter = NSDateFormatter()
>         dateStringFormatter.calendar = NSCalendar(calendarIdentifier:
> NSCalendarIdentifierGregorian)
>         dateStringFormatter.dateFormat = "yyyy-MM-dd HH:mm"
>         dateStringFormatter.locale = NSLocale(localeIdentifier:
> "en_US_POSIX")
>         dateStringFormatter.timeZone = NSTimeZone.localTimeZone()
>         let d = dateStringFormatter.dateFromString(dateString)!
>         self.init(timeInterval:0, sinceDate:d)
>     }
> }
>
> ...
>
> let calendar = NSCalendar.currentCalendar()
>         calendar.timeZone = NSTimeZone.localTimeZone()
>         calendar.locale = NSLocale(localeIdentifier: "en_US_POSIX")
>         let startDate = NSDate(dateString:"2015-12-08 7:30")
>         let date = calendar.dateByAddingUnit(.Minute, value: 5, toDate:
> startDate, options: [])
> *        print (date!) //This is way off. How to fix this?*
>
>
> A NSDateā€™s description aways prints its time in UTC, not the local time
> zone. If you print the result of a properly constructed NSDateFormatter you
> should see the correct results.
>
>
>
>
> On Tue, Dec 8, 2015 at 3:54 PM David Duncan <david.dun...@apple.com>
> wrote:
>
>>
>> > On Dec 8, 2015, at 12:14 PM, Eric E. Dolecki <edole...@gmail.com>
>> wrote:
>> >
>> > Hey all,
>> >
>> > I am being supplied a time (not day specific). Say "7:30 AM" - as a
>> string.
>> > Could be PM.
>> >
>> > I need to make a slider with a range of 1 minute to 120 mins. Moving the
>> > slider adjusts the 7:30 AM display. So I am adding minutes to the
>> display
>> > time. What's the best way to do this? I am using Swift.
>>
>> NSCalendar and NSDateComponents are still the best way to do these types
>> of time modifications (with NSDateFormatter for display).
>>
>> >
>> > I'll be googling too.
>> >
>> > Thanks,
>> > Eric
>> > _______________________________________________
>> >
>> > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
>> >
>> > Please do not post admin requests or moderator comments to the list.
>> > Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>> >
>> > Help/Unsubscribe/Update your Subscription:
>> >
>> https://lists.apple.com/mailman/options/cocoa-dev/david.duncan%40apple.com
>> >
>> > This email sent to david.dun...@apple.com
>>
>> --
>> David Duncan
>>
>>
> --
> David Duncan
>
>
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to