NSDateComponents NSDate Problem

综合技术 Hello, buddy! (源链)

i have a string like 2013-03-05T07:37:26.853
and i want to get the Output : 2013-03-05 07:37:26
. I want the final output in NSDate
object. i am using below function to convert desire output. and it returning wrong time.

-  (NSDate *) getDateFromCustomString:(NSString *) dateStr
{    NSArray *dateStrParts = [dateStr componentsSeparatedByString:@"T"];
    NSString *datePart = [dateStrParts objectAtIndex:0];
    NSString *timePart = [dateStrParts objectAtIndex:1];
    NSArray *dateParts = [datePart componentsSeparatedByString:@"-"];
    NSArray *timeParts = [timePart componentsSeparatedByString:@":"];

NSDateComponents *components = [[NSDateComponents alloc] init];

[components setHour:[[timeParts objectAtIndex:0] intValue]];
[components setMinute:[[timeParts objectAtIndex:1] intValue]];
[components setSecond:[[timeParts objectAtIndex:2] intValue]];

[components setYear:[[dateParts objectAtIndex:0] intValue]];
[components setMonth:[[dateParts objectAtIndex:1] intValue]];
[components setDay:[[dateParts objectAtIndex:2] intValue]];

NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDate *date = [currentCalendar dateFromComponents:components];
NSLog(@"Date 1:%@",date); // Returns wrong time (2013-03-05 02:07:26)

/* i had tried the below.
NSDateFormatter * format = [[NSDateFormatter alloc] init];
[format setTimeZone:NSTimeZoneNameStyleStandard];
[format setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

NSLog(@"Date 2:%@",[format stringFromDate:date]); // Returns correct date (2013-03-05 07:37:26)

NSDate *fDate = [format dateFromString:[format stringFromDate:date]];
DLog(@"Date 3:%@",fDate); // Returns wrong time (2013-03-05 02:07:26)
*/

[components release];
return  date;
}

Plz suggest me if any idea.

Try this : issue is Time zone.

+ (NSDate *) getDate:(NSString *) dateStr
{
    NSArray *dateStrParts = [dateStr componentsSeparatedByString:@"T"];
    NSString *datePart = [dateStrParts objectAtIndex:0];
    NSString *timePart = [dateStrParts objectAtIndex:1];

    NSString *t = [[timePart componentsSeparatedByString:@"."] objectAtIndex:0];
    NSString *newDateStr = [NSString stringWithFormat:@"%@ %@",datePart,t];
    //2013-03-05 07:37:26

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [df dateFromString:newDateStr];
    DLog(@"%@",date);

    return  date;
}

您可能感兴趣的

Iphone: the use of background location s... i'm trying to get a location and compare it with a location online all that happen in background the method i create is working fine using background ...
New Updates to BluePic! We are excited to announce that our team has just made several updates to the BluePic application. For those who are not familiar with BluePic, we bu...
Airprint iOS Tutorial With Airprint-enabled printers it is possible to print wireless from an iPhone or an iPad. In this tutorial the text within a Text View will be printe...
iOS 开发-NSURLSession设置代理NSURLSessionDataDelegate... 方法截图: NSURLSession.png 上一篇我们说到,一般情况下我们都直接使用下面的那两个带有block回调的方法来发起网络请求。然而,我们获取不了数据的进度,这时就需要我们采用代理。 以GET方法为例,设置代理 #import "Vi...
iOS 搭建 React Native 开发环境 一、ReactNative简介 ReactNative是移动端目前最热的框架之一, 着力于提高多平台开发的开发效率 —— 仅需学习一次,编写任何平台。(Learn once, write anywhere)。Facebook 已经在多项产品中使用了React Native,并且将持续地投入...
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » NSDateComponents NSDate Problem



专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录