• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Best way to parse a time today?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Best way to parse a time today?


  • Subject: Re: Best way to parse a time today?
  • From: Roger Dalal <email@hidden>
  • Date: Wed, 12 Oct 2011 21:46:05 -0400

Rick:

The following code, which is likely what you are doing, will return the time in 1970 (NSDate's reference date) because you have not specified a date:

NSString *timeString = @"14:50 PDT";
NSDateFormatter *df = [[NSDateFormatter alloc ] init];
[df setDateFormat:@"HH':'mm zzz"];
NSDate *date = [df dateFromString:timeString];
[df release];


Instead, you need to use date components to set the day as well as the time, per the following:

NSString *timeString = @"14:50 PDT";
NSDateFormatter *df = [[NSDateFormatter alloc ] init];
[df setDateFormat:@"HH':'mm zzz"];
NSDate *date = [df dateFromString:timeString];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit ) fromDate:[NSDate date]];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate: date ];

[dateComponents setHour:[timeComponents hour]];
[dateComponents setMinute:[timeComponents minute]];
[dateComponents setSecond:[timeComponents second]];

NSDate *timeToday = [calendar dateFromComponents:dateComponents];
[df release];

Change 'fromDate' in NSDateComponents *dateComponents to whatever date you want in order to create your time on a different day.

Best Wishes.

Roger Dalal




On Oct 12, 2011, at 9:30 PM, Rick Mann wrote:

> I have a situation where I have to parse times like "14:50 PDT". If I just set up an NSDateFormatter with dateFormat = @"HH:mm z", I end up with a time of day in 1970.
>
> What's the best way to get it to give me that time of day today?
>
> Thanks,
> Rick
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> 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:
>
> This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Best way to parse a time today?
      • From: Dave DeLong <email@hidden>
References: 
 >Best way to parse a time today? (From: Rick Mann <email@hidden>)

  • Prev by Date: Best way to parse a time today?
  • Next by Date: Re: Best way to parse a time today?
  • Previous by thread: Best way to parse a time today?
  • Next by thread: Re: Best way to parse a time today?
  • Index(es):
    • Date
    • Thread