简体   繁体   English

使用NSDateFormater从字符串获取日期时,如何忽略某些字符? 另外,NSDateFormatter有参考吗?

[英]How do I ignore some characters when getting date from string using NSDateFormater? Also, is there any reference for NSDateFormatter?

My problem is simple. 我的问题很简单。 I have strings in the format "16th Sep 2015" 我有“ 2015年9月16日”格式的字符串

When generating the date string, I used the NSDateFormatter as "d MMM yyyy" and then manually modified it to insert the date suffixes using a switch case. 生成日期字符串时,我将NSDateFormatter用作“ d MMM yyyy”,然后手动对其进行了修改,以使用切换大小写插入日期后缀。

Now, I need to extract date again. 现在,我需要再次提取日期。

Is there any way to ignore those 2 letters? 有什么办法可以忽略这两个字母吗?

Also, I have been learning NSDateFormatter by using Google and just following posts like this but all my questions are not answered and sometimes, there is a mismatch in behaviour described. 另外,我一直在使用Google学习NSDateFormatter,并且仅关注类似这样的帖子,但是我的所有问题都没有得到回答,有时,描述的行为不匹配。

Is there a standard reference by Apple where the Date Formatter codes are described? Apple是否有描述日期格式程序代码的标准参考书?

NSDateformatter does not support days with ordinal indicators, but you could use a regular expression to strip the indicators NSDateformatter不支持带有序号指示符的日期,但是您可以使用正则表达式删除指示符

var dateString = "16th Sep 2015"
if let range = dateString.range(of: "(st|nd|rd|th)", options: .regularExpression) {
    dateString.removeSubrange(range)
}    
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "dd MMM yyyy"
let date = formatter.date(from: dateString)!
print(date)

Or in Objective-C 或在Objective-C中

NSMutableString *dateString = [NSMutableString stringWithString:@"16th Sep 2015"];

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(st|nd|rd|th)" options:nil error:nil];
[regex replaceMatchesInString:dateString options: nil range: NSMakeRange(0, 4) withTemplate:@""];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"d MMM yyyy";
NSDate *date = [formatter dateFromString:dateString];
NSLog(@"%@", date);

This isn't the prettiest of solutions but it can work: 这不是解决方案中最漂亮的方法,但它可以起作用:

NSString *dateString = @"1st Sep 2015";

dateString = [dateString stringByReplacingOccurrencesOfString:@"st " withString:@" "];
dateString = [dateString stringByReplacingOccurrencesOfString:@"nd " withString:@" "];
dateString = [dateString stringByReplacingOccurrencesOfString:@"rd " withString:@" "];
dateString = [dateString stringByReplacingOccurrencesOfString:@"th " withString:@" "];

NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateFormat = @"d MMM yyyy";

NSDate *date = [df dateFromString:dateString];

Only a partial answer, I am afraid, but Apple essentially implements this specification: http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns 恐怕只有部分答案,但是Apple本质上实现了此规范: http : //www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns

The specification does suggest something called "Lenient parsing" that allows the parser to ignore some extraneous characters, but at least as defined in the standard, this doesn't seem to extend to your particular case. 该规范确实建议了一种称为“ Lenient parsing”的方法,该方法允许解析器忽略一些多余的字符,但是至少按照标准中的定义,这似乎并没有扩展到您的特定情况。 So it certainly looks like you have to manually remove the suffix from the day number before handing the string to the parser, unless it happens to "just work", which I don't think it does. 因此,肯定看起来您必须在将字符串交给解析器之前从日期数字中手动删除后缀,除非它碰巧“正常工作”,我认为这样做不会。

While the will work if you have a month that is 3 letters, it will not work if you have the full month. 如果您的月份为3个字母,则可以使用,但是如果您使用了一个完整的月份,则将无法使用。 For example, "Augu" will turn into "August" Using Regular Expressions, you can check for a digit to be before the letters by doing the following: 例如,“ Augu”将变成“ August”。使用正则表达式,您可以通过执行以下操作来检查字母前的数字:

var dateString = "16th Sep 2015"
//regular expression using positive look behind. Checks for a digit, than a two letter combination that will match below
dateString = dateString.replacingOccurrences(of: "(?<=\\d)[snrt][tdh]", with: "", options: String.CompareOptions.regularExpression, range: nil)

let df = DateFormatter()
df.dateFormat = "d MMM yyyy" //if full month, add extra M
df.date(from: dateString)

Here is the code with CORRECT date format that will be your angel: 这是具有正确日期格式的代码,它将成为您的天使:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateFormat = @"d'th' MMM yyyy";

NSString *stringDate = [df stringFromDate:[NSDate date]];

//output -- 16th Sep 2015

Now you can use this dateformatter to convert to and from NSDate object. 现在,您可以使用此dateformatter在NSDate对象之间进行转换。

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

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