简体   繁体   English

来自iPhone中NSString的NSDate

[英]NSDate from NSString in iPhone

One simple thing on conversion from NSString to NSDate . NSStringNSDate转换很简单。 How can I convert Mon, 27 August 2012 01:30 AM to NSDate in this same format. 我如何以相同的格式将Mon, 27 August 2012 01:30 AM转换为NSDate I tried with NSDateFormatter . 我尝试使用NSDateFormatter But I am not getting it in this required format. 但是我没有以这种要求的格式得到它。 Can anyone help? 有人可以帮忙吗? This is what I tried. 这就是我尝试过的。

 NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:@"EEEE,dd MM yyyy HH:mm a"];
NSDate *date1 = [df dateFromString:@"Mon,27 August 2012 01:30 AM"];
NSLog(@"%@",date1);

I hope this will help you sure! 希望对您有帮助!

NSDateFormatter *dateformater=[[[NSDateFormatter alloc] init]autorelease];
[dateformater setDateFormat:@"EEEE,dd MMMM yyyy HH:mm a"];

NSDate *todayTmp=[NSDate date];
NSString *conversionDate=[dateformater stringFromDate:todayTmp];

Note : (Upper case) HH for 24h time format, (Lower case) hh for 12h time format
NSString *myDateAsAStringValue = @"Mon, 27 August 2012 01:30 AM";

NSDateFormatter *df = [[NSDateFormatter alloc] init];

[df setDateFormat:@"EEE, dd MMM yyyy HH:mm a"];

NSDate *myDate = [[NSDate alloc]init];

myDate = [df dateFromString:myDateAsAStringValue];

[df release];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm a"];
NSString *strDate = [dateFormatter stringFromDate:myDate];
NSLog(@"%@", strDate);
[dateFormatter release];

please use below code 请使用下面的代码

   NSDateFormatter *df=[[NSDateFormatter alloc] init];
   [df setDateFormat:@"EEE,dd MMMM yyyy hh:mm a"];
   NSDate *date1 = [df dateFromString:@"Mon,27 August 2012 01:30 AM"];
   NSLog(@"%@",date1);

your formatter is wrong check this one 您的格式化程序错了检查此一项

NSDateFormatter is to specify the format that will appear in the date-string when extracting string from date or the format that is in the date-string when extracting date from string NSDateFormatter用于指定从日期提取字符串时在日期字符串中出现的格式,或从字符串中提取日期时在日期字符串中出现的格式

So whenever you extract NSDate from a NSString, NSDate is always obtained in default date format(eg 2012-08-27 00:30:00 +0000 )... only the when you extract NSString from NSDate, NSString can be obtained in desired(custom) format that you set in NSDateFormatter. 因此,每当您从NSString中提取NSDate时,总是以默认的日期格式获取NSDate(例如2012-08-27 00:30:00 +0000 )...仅当您从NSDate中提取NSString时,才可以按需要获取NSString。您在NSDateFormatter中设置的(自定义)格式。

NSLog will return NSDate in a fixed format, i guess. 我想NSLog将以固定格式返回NSDate。 If we need Date in different format, we should have to format it via NSDateFormatter and get it as NSString. 如果需要不同格式的Date,则必须通过NSDateFormatter对其进行格式化,并将其作为NSString获取。 Just a guess. 只是一个猜测。

Don't forget to set the correct locale! 不要忘记设置正确的语言环境! If your device does not use an english locale NSDateFormatter can have problems to convert Mon and August into useful information because Mon is not the correct abbreviation for Monday in your language. 如果您的设备未使用英语语言环境,则NSDateFormatter可能无法将MonAugust转换为有用的信息,因为在您的语言中Mon不是星期一的正确缩写。 For example in Germany the correct three letter abbreviation for Monday is Mon. 例如在德国,星期一的正确的三个字母缩写为星期一Mon. .

If you parse dates that have words in it you have to set the correct locale. 如果解析日期中包含单词的日期,则必须设置正确的语言环境。

This should work: 这应该工作:

NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:@"EEE,dd MMMM yyyy hh:mm a"];

NSLocale *posixLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[df setLocale:posixLocale];

NSDate *date1 = [df dateFromString:@"Mon,27 August 2012 01:30 AM"];
NSLog(@"%@",date1);

EEE is the dateformatter code for a three letter weekday abbreviation. EEE是工作日三字母缩写的dateformatter代码。
hh is the dateformatter code for Hours between 1 and 12. HH means 0-23 hh是1到12之间的小时的日期格式代码。HH表示0-23
MMMM is the full month, MM would be the numeric value (= 08) of the month. MMMM是整月,MM是该月的数值(= 08)。

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

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