开发者

Converting Date+Time Formats

开发者 https://www.devze.com 2023-03-03 09:42 出处:网络
I need to convert the following format into a new format using NSDateFormatter. \'Fri, 25 Mar 2011 10:16:00 -0700\'.

I need to convert the following format into a new format using NSDateFormatter.

'Fri, 25 Mar 2011 10:16:00 -0700'.

I tried using "aaa, dd bbb YYYY HH:MM:SS ZHHMM" as format, but it doesn't work; it gives me a date way in the past.

I also need to convert it into the Eastern Time Zone when creating a new date.

The code I used is the following one:

NSDateFormatter *newDateFormatter 开发者_运维技巧= [[NSDateFormatter alloc] init]; 
[newDateFormatter setDateFormat:@"dd MMM YYYY HH:mm:SS"];


Let's break this string down into the various portions:

Fri, 25 Mar 2011 10:16:00 -0700 becomes:

  • Fri: abbreviated day of the week
  • 25: potentially zero-padded day of the month
  • Mar: abbreviated month
  • 2011: year
  • 10: potentially zero-padded hour (in 24-hour format because there is no AM/PM)
  • 16: potentially zero-padded minute
  • 00: zero-padded second
  • -0700: time zone offset

Now let's look at the date format patterns that NSDateFormatter supports:

  • abbreviated day of the week: EEE
  • zero-padded day of the month: dd
  • abbreviated month: MMM
  • year: y
  • zero-padded hour (24-hour): HH
  • zero-padded minute: mm
  • zero-padded second: ss
  • time zone offset: ZZZ

Thus, your format string should be: @"EEE, dd MMM y HH:mm:ss ZZZ".

0

精彩评论

暂无评论...
验证码 换一张
取 消