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

醉酒当歌 提交于 2019-12-11 16:54:48

问题


HI, i have a string 2011-05-13T00:00:00 and i want to convert this string in MMM d, yyyy format. any Help?


回答1:


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];



回答2:


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




回答3:


use this code

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

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

NSDate *myDate = [df dateFromString: myDateAsAStringValue];




回答4:


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];

}


来源:https://stackoverflow.com/questions/5992635/date-format-problem-convert-string-to-date-in-mmm-d-yyyy-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!