将日期的NSString转换为NSDate [英] Convert NSString of a date to an NSDate

查看:102
本文介绍了将日期的NSString转换为NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但我似乎无法在此处或文档中找到答案。

This might be a silly question, but I can't seem to find the answer on here or in the documentation.

我想将NSString(例如@9/22/2010 3:45 PM)转换为NSDate。

I want to convert an NSString such as @"9/22/2010 3:45 PM" to an NSDate.

我知道要使用NSDateFormatter,但问题是

I know to use NSDateFormatter, but the problems are


  1. 这个月可能是一两位数

  2. 同样,日期可以是一位或两位数

  3. 小时数可以是一位或两位数

  4. 我该怎么办? / PM?

  1. The month could be one or two digits
  2. Likewise, the date could be one or two digits
  3. Hours could be one or two digits
  4. What do I do about AM/PM?


推荐答案

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy h:mm a"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat release];

2位数日或2位数月没有问题。

there is no problem in 2 digit day or 2 digit month.

这必须帮助你。

这篇关于将日期的NSString转换为NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆