如何将 NSstring 转换为 Nsdate
How to convert NSstring to Nsdate
我收到来自服务的回复
"created_at" = "Thu Jul 06 07:26:23 +0000 2017";
我想把字符串转成Nsdate格式
我不知道该怎么做
试试这个:
NSString *dateString = @"Thu Jul 06 07:26:23 +0000 2017";
NSDate *date =[[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"EEE MMM dd hh:mm:ss Z yyyy";
date = [ dateFormatter dateFromString:dateString];
使用此代码
(source::http://nsdateformatter.com)
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEE MMM dd hh:mm:ss Z yyyy"];
NSDate *date = [dateFormat dateFromString:[NSString stringWithFormat:@"Thu Jul 06 07:26:23 +0000 2017"]];
NSLog(@"date is %@ ",date);
目标 C 实现
NSString *dateString = @"Thu Jul 06 07:26:23 +0000 2017";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *date =[[NSDate alloc] init];
dateFormatter.dateFormat = @"EEE MMM DD hh:mm:ss Z yyyy";
date = [dateFormatter dateFromString: dateString]; //generates date from dateString
Swift 实施如下
let dateString = "Thu Jul 06 07:26:23 +0000 2017"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE MMM DD hh:mm:ss Z yyyy"
dateFormatter.date(from: dateString) //"Jan 6, 2017, 12:56 PM"
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"E MMM d HH:mm:ss Z yyyy"];
NSDate *date = [f dateFromString:@"Thu Jul 06 07:26:23 +0000 2017"];
试试这个代码:
NSString *dateString = @"2018-07-27 03:08:10";
NSDate *date =[[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-mm-dd hh:mm:";
date = [ dateFormatter dateFromString:dateString];
我收到来自服务的回复
"created_at" = "Thu Jul 06 07:26:23 +0000 2017";
我想把字符串转成Nsdate格式 我不知道该怎么做
试试这个:
NSString *dateString = @"Thu Jul 06 07:26:23 +0000 2017";
NSDate *date =[[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"EEE MMM dd hh:mm:ss Z yyyy";
date = [ dateFormatter dateFromString:dateString];
使用此代码
(source::http://nsdateformatter.com)
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEE MMM dd hh:mm:ss Z yyyy"];
NSDate *date = [dateFormat dateFromString:[NSString stringWithFormat:@"Thu Jul 06 07:26:23 +0000 2017"]];
NSLog(@"date is %@ ",date);
目标 C 实现
NSString *dateString = @"Thu Jul 06 07:26:23 +0000 2017";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *date =[[NSDate alloc] init];
dateFormatter.dateFormat = @"EEE MMM DD hh:mm:ss Z yyyy";
date = [dateFormatter dateFromString: dateString]; //generates date from dateString
Swift 实施如下
let dateString = "Thu Jul 06 07:26:23 +0000 2017"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE MMM DD hh:mm:ss Z yyyy"
dateFormatter.date(from: dateString) //"Jan 6, 2017, 12:56 PM"
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"E MMM d HH:mm:ss Z yyyy"];
NSDate *date = [f dateFromString:@"Thu Jul 06 07:26:23 +0000 2017"];
试试这个代码:
NSString *dateString = @"2018-07-27 03:08:10";
NSDate *date =[[NSDate alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-mm-dd hh:mm:";
date = [ dateFormatter dateFromString:dateString];