开发者

Date Format problem - convert string to date in MMM d, yyyy format

HI,开发者_开发知识库 i have a string 2011-05-13T00:00:00 and i want to convert this string in MMM d, yyyy format. any Help?


Read this: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1

I'm not at my Mac, but this should give you a head-start:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'"];
NSDate *originalDate = [dateFormatter dateFromString:originalDateString];
[dateFormatter setDateFormat:@"MM' 'DD','yyyy"];
NSString *formattedDateString = [dateFormatter stringFromDate:originalDate];
[dateFormatter release];


You should look at the reference documentation for NSDateFormatter, it do exactly what you want: http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html


use this code

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

[df setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];

NSDate *myDate = [df dateFromString: myDateAsAStringValue];


On Class where Date format is required (Implementation class)

-(void)DateFormatter:stringDateToBeFormatted
{
   NSDate *dateFormatter=[NSDate convertStringToDate:stringDateToBeFormatted];
   NSString *requiredDateFormat= [NSDate dateStringWithMediumFormat: dateFromatter];

self.stringDateFormatLable.Text=[NSString stringWithFormat:@"date required %@",requiredDateFormat]
}


On NSDate Class implementation class

+(NSString *)dateStringWithMediumFormat:(NSDate *)date
{
    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc]init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
or [dateFormatter setDateFormat:@"MMM-dd-yyyy"];
    return [dateFormatter stringFromDate:date];
}

+(NSDate *)convertStringToDate:(NSString *)dateString
{
    NSDateFormatter *dateFormatter= [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'hh':'mm':'ss"];
    return [dateFormatter dateFromString:dateString];

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜