【问题标题】:The Date format in iOS is not clear...iOS中的日期格式不清楚...
【发布时间】:2016-01-06 11:45:47
【问题描述】:
  NSString* lastModified  =   @"26 Jun 2015 11:16";
  activityTime           =   [dateObject getDateFromString:lastModified andFormat:@"dd MMM yyyy HH:mm" toFormat:@"dd MMM, yyyy"];

我使用的格式有什么问题?我总是将 activityTime 变量设为 nil。

我还检查了苹果文档...我的用例给出了相同的格式。但它不起作用。

需要对此有一些见解!

提前谢谢...

【问题讨论】:

  • 格式对我来说还可以。是getDateFromString:andFormat:toFormat: 来自iOS API。我不认识它。

标签: ios date


【解决方案1】:
NSString *str =  @"26 Jun 2015 11:16"; /// here this is your date with format yyyy-MM-dd

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:@"dd MMM yyyy HH:mm"]; //// here set format of date which is in your output date (means above str with format)

NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format

dateFormatter = [[NSDateFormatter alloc] init] ;
[dateFormatter setDateFormat:@"dd MMM, yyyy"];// here set format which you want...

NSString *convertedString = [dateFormatter stringFromDate:date]; //here convert date in NSString
NSLog(@"Converted String : %@",convertedString);

【讨论】:

  • 您也可以在该方法中添加上述代码并传递两个参数
猜你喜欢
  • 2015-12-26
  • 2021-11-30
  • 1970-01-01
  • 2014-02-24
  • 1970-01-01
  • 2022-01-25
  • 2018-08-24
  • 1970-01-01
  • 2022-01-23
相关资源
最近更新 更多