• 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: Date and Time Conversion with TimeZone
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Date and Time Conversion with TimeZone


  • Subject: Re: Date and Time Conversion with TimeZone
  • From: Mike Abdullah <email@hidden>
  • Date: Wed, 2 Jul 2008 11:23:19 +0100

On 1 Jul 2008, at 18:20, email@hidden wrote:


Hello,

I have a date event returned as a date time string from a feed in the
following format,

"Tue 01 Jul 2008 15:00:00 +0100"

I'm assuming this is GMT plus one hour, I'd like to convert this string to
the to a more user friendly date time string such as,


"Today at 9:00am" or "Tue Jul 01, 2008 9:00am"

that would take into account the current Time Zone setting of the Mac
displaying the date event. I've looked at the NSDateFormatter and
NSCalendarDate, but didn't see anything that takes into account the Time
Zone setting on the current Mac. Does anyone know of where I might find or
have a sample of a code snippet that does something similar to this?



So there are two parts to your problem. First you need to take the original string and create an NSDate object from it. To do this, create an NSDateFormatter and set up its -dateFormat property to match the "Tue 01 Jul 2008 15:00:00 +0100" format. This link may be handy for formatting info:


file:///Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.CoreReference.docset/Contents/Resources/Documents/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html #//apple_ref/doc/uid/TP40002369-DontLinkElementID_13

Now that you have a NSDate object, you'll want a second NSDateFormatter that converts the date into the desired string format. For the most part, you can achieve the "Tue Jul 01, 2008 9:00am" style dates with -setDateStyle: and -setTimeStyle: I would STRONGLY recommend you use this rather than try to specify your own date format string as it means dates will match the user's preferences.

You can then subclass NSDateFormatter to supplement the behaviour to generate "Today" etc. when appropriate. For all I know, there may exist a 3rd party NSDateFormatter subclass to do this already.

Mike.
_______________________________________________

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


References: 
 >Date and Time Conversion with TimeZone (From: email@hidden)

  • Prev by Date: Re: Bindings - registering change notification for multiple keys
  • Next by Date: Re: Bindings - registering change notification for multiple keys
  • Previous by thread: Date and Time Conversion with TimeZone
  • Next by thread: RE: Newbie question: Timers
  • Index(es):
    • Date
    • Thread