ObjC中的Date Formatter问题 [英] Date Formatter issue in ObjC

查看:79
本文介绍了ObjC中的Date Formatter问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将日期字符串2015-03-10 00:00:00.000(字符串从Web服务)转换为NSDate。我使用这样的代码

  NSDateFormatter * fromDateFormat = [[NSDateFormatter alloc] init]; 
[fromDateFormat setDateFormat:@yyyy-MM-dd HH:mm:ss];
NSDate * outputDate = [fromDateFormat dateFromString:@2015-03-10 00:00:00.000];

outputDate为零。我试过没有最后三个零,它正在工作。但是我不能删除那个零,因为它来自一个服务。

解决方案

函数

  NSDate * current = [self convertStringToDate:data.strCreateDate formate:@yyyy-MM-dd HH:mm:ss ]; 

将此方法放在项目中



<$ p (NSState *)日期格式:(NSString *)formate {
NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init]; $ p> - (NSDate *)convertStringToDate
[dateFormat setDateFormat:formate];
return [dateFormat dateFromString:date];
}

这对你有帮助..


I need to convert a date string "2015-03-10 00:00:00.000"(string from web service) to NSDate. I have used code like this

NSDateFormatter *fromDateFormat = [[NSDateFormatter alloc] init];
[fromDateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];      
NSDate *outputDate = [fromDateFormat dateFromString:@"2015-03-10 00:00:00.000"];

outputDate is nil. I tried without last triple zeros, and it is working. but I can't remove that zeros, because it is from a service.

解决方案

If you want convert string to date using This function

NSDate *current=[self convertStringToDate:data.strCreateDate formate:@"yyyy-MM-dd HH:mm:ss"];

Put this method in your projects

- (NSDate *)convertStringToDate: (NSString *)date formate:(NSString *)formate {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:formate];
    return [dateFormat dateFromString:date];
}

This is helpfult to you..

这篇关于ObjC中的Date Formatter问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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