将字符串转换为NSDate


Answers:


197
NSString *dateStr = @"20100223";

// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd"];
NSDate *date = [dateFormat dateFromString:dateStr];  

// Convert date object to desired output format
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];  
[dateFormat release];

希望这会帮助你。


15
在此处找到了说明所有格式值的文章:alexcurylo.com/blog/2009/01/29/nsdateformatter-formatting
mtmurdock 2012年


您知道为什么小写字母yyyy和dd起作用而大写字母MM起作用吗...?
Rishabh Tayal

9

您将要看看NSDateFormatter。确定日期字符串的格式,然后用于dateFromString:将字符串转换为NSDate对象。


5
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/YYYY"];
NSDate *date = [dateFormat dateFromString:dateStr];  
list = [self getYear:date];


- (NSMutableArray *)getYear:(NSDate*)date
{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];

    int year = [components year];
    int month = [components month];
    int day = [components day];
    NSLog(@"%d",year);

    NSMutableDictionary *dateDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%d", day], @"day", [NSString stringWithFormat:@"%d", month], @"month", [NSString stringWithFormat:@"%d", year], @"year", nil];

    return dateDict;
}

返回类型是NSMutableArray,但是您返回的NSMutableDictionary为什么呢?
kiran '18

2

一个简单的方法,即使您有sqlite DB:

// NSTimeInterval is basically typedef of double so you can receive it as double also.
NSTimeInterval *current = [[NSDate date] timeIntervalSince1970]; 
[DB addToDB:current];

//retrieve the date from DB
NSDate *retrievedDateItem = [[NSDate alloc] initWithTimeIntervalSince1970:[getDBValueAsDouble]]; //convert it from double (NSTimeInterval) to NSDate
// Set the style
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
// Converted to string
NSString *convertedDate = [dateFormatter stringFromDate:retrievedDateItem];

此示例的输出:

2015年8月3日,下午12:27:50


2

我在Swift中的工作版本

func dateFor(timeStamp: String) -> NSDate
{
    let formater = NSDateFormatter()
    formater.dateFormat = "HH:mm:ss:SSS - MMM dd, yyyy"
    return formater.dateFromString(timeStamp)!
}


func timeStampFor(date: NSDate) -> String
{
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "HH:mm:ss:SSS - MMM dd, yyyy"

    return dateFormatter.stringFromDate(date)
}

1
+(NSDate*)str2date:(NSString*)dateStr{
    if ([dateStr isKindOfClass:[NSDate class]]) {
        return (NSDate*)dateStr;
    }

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [dateFormat dateFromString:dateStr];
    return date;
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.