【问题标题】:Date format does not work properly日期格式无法正常工作
【发布时间】:2012-02-16 07:28:50
【问题描述】:

我将日期记入stringdate format 是这样,string

datenotification=20-02-2012

现在我想把它改成

20-feb-2012

但出于检查目的,我只是将其放入 dateformatter 并打印出来,但它显示的日期不正确

NSDateFormatter* currentdateformate = [[NSDateFormatter alloc] init];
[currentdateformate setDateFormat:@"dd-MM-YYYY"];
NSDate *d=[currentdateformate dateFromString:dateNotification];
NSString *str3=[currentdateformate stringFromDate:d];
[currentdateformate release];

【问题讨论】:

  • d的输入请写在这里。

标签: iphone objective-c ios ipad ios4


【解决方案1】:

嘿,看看这个例子

 NSString *string = @"12-02-2000";
 NSDateFormatter* currentdateformate = [[NSDateFormatter alloc] init];
 [currentdateformate setDateFormat:@"dd-MM-yyyy"];
 NSDate *d=[currentdateformate dateFromString:string];
 [currentdateformate setDateFormat:@"dd-MMM-yyyy"];
 NSString *str3=[currentdateformate stringFromDate:d];
 [currentdateformate release];
 NSLog(@"S %@",str3);

输出 S 2012 年 2 月 12 日

【讨论】:

  • 不要使用大写“YYYY”,因为使用小“yyyy”字母会得到错误的结果
猜你喜欢
  • 2014-10-07
  • 2019-04-10
  • 1970-01-01
  • 2018-05-12
  • 1970-01-01
  • 2018-02-13
  • 1970-01-01
  • 1970-01-01
  • 2019-09-06
相关资源
最近更新 更多