简体   繁体   English

我在此日期格式化程序中做错了什么

[英]What am I doing wrong in this date formatter

 NSDate *date=[[NSDate alloc] init];
    NSDateFormatter *dfLocal=[[NSDateFormatter alloc]init];
    [dfLocal setDateFormat:@"yyyy-MM-dd"];
    date=[dfLocal dateFromString:@"2011-09-22"];
    NSLog(@"the date is %@",date);

this is my code and gives the output as.. 这是我的代码,并给出输出为..

2012-04-02 18:13:45.338 TimeProblem[2656:207] the date is 2011-09-21 18:30:00 +0000

why is it appending this.. 18:30:00 +0000 to the end..? 为什么将其附加到此。.18:30:00 +0000结束..? I just want it to be as 00:00:00 +0000 我只希望它是00:00:00 +0000

Use below: 在下面使用:

NSDate *date=[[NSDate alloc] init];
NSDateFormatter *dfLocal=[[NSDateFormatter alloc]init];
[dfLocal setDateFormat:@"yyyy-MM-dd"];
[dfLocal setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
date=[dfLocal dateFromString:@"2011-09-22"];

NSLog(@"the date is %@",date);

You are in GMT+5.30 . 您处于GMT + 5.30中。 That is why you get the time offset. 这就是为什么您会得到时间偏移。 It is a timeZone problem. 这是一个时区问题。 Adding this line would solve the problem. 添加此行将解决问题。

 [dfLocal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

hi you want to get date and time then use like this . 您好,您想获取日期和时间,然后像这样使用。

[dfLocal setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM