日期和月份名称的 NSDateFormatter

NSDateFormatter for day and month name

我正在尝试以 2015 年 3 月 9 日星期一的格式获取日期。但以下代码未返回所需的日期格式。我想我使用了错误的格式化程序。这是代码:

NSString *dateString = @"09-03-2015";
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd MMMM yyyy"];
        NSDate *date = [[NSDate alloc] init];
        date = [dateFormatter dateFromString:dateString];
        NSLog(@"%@",[dateFormatter stringFromDate:date]);

首先,您必须将 dateString 转换为 NSDate

NSString *dateString = @"09-03-2015";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
NSLog(@"%@",[dateFormatter stringFromDate:date]);

之后,您可以使用此格式:@"EEEE,MMMM dd, yyyy"dateValue 转换为日期字符串,例如 Monday, March 09, 2015
希望这有帮助
Helpful link 给你

尝试将 dateFormat 属性 设置为 @"dd'-'MM'-'yyyy" 。始终检查 unicode date symbol table。 "MMMM" 表示日期月份应该是完整的月份名称。

试试这个...

//Getting date from string
    NSString *dateString = @"09-03-2015";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSDate *date = [[NSDate alloc] init];
    date = [dateFormatter dateFromString:dateString];
// converting into our required date format    
    [dateFormatter setDateFormat:@"EEEE, MMMM dd, yyyy"];
    NSString *reqDateString = [dateFormatter stringFromDate:date];
    NSLog(@"date is %@", reqDateString);

LOG:2015-03-09 12:40:33.456 TestCode [1377:38775] date is Monday, March 09, 2015

试试这个..

    NSString * yourJSONString = @"09-03-2015";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];;
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    [dateFormatter setLocale:[NSLocale currentLocale]];
    NSDate *dateFromString = [dateFormatter dateFromString:yourJSONString];
    [dateFormatter setDateFormat:@"EEEE,LLLL dd, yyyy"];
    NSString *output = [dateFormatter stringFromDate:dateFromString];
    NSLog(@"%@", output);

试试这个:-

NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSString * format = [NSDateFormatter dateFormatFromTemplate:@"EEEEMMMMdyyyy" options:0 locale:[NSLocale currentLocale]];
[formatter setDateFormat:format];

NSString *dateFormatted = [fullDateFormatterTime stringFromDate: date];
NSLog(@"Formatted date: %@", dateFormatted);