objective c - en_us_posix - ¿Convertir la fecha en formato MM/dd/aaaa en xcode?
nsdateformatter objective c (9)
La respuesta original aceptada hace algunas cosas adicionales:
1) Asigna dos instancias de NSDateFormatter que no son necesarias. NSDateFormatter es caro de crear.
2) Es mejor lanzar los formateadores de fecha explícitamente cuando haya terminado con ellos en lugar de una versión diferida con autorelease.
//get datepicker date (NSDate *)
NSDate *datePickerDate = [myDatePicker date];
//create a date formatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//set its format as the required output format
[formatter setDateFormat:@"MM/dd/yyyy"];
//get the output date as string
NSString *outputDateString = [formatter stringFromDate:datePickerDate];
//release formatter
[formatter release];
Tengo un UIDatePicker
y UIDatePicker
la fecha seleccionada en formato yyyy-MM-dd
. Ahora quiero convertirlo a formato MM/dd/yyyy
. ¿Cómo puedo hacerlo?
NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease];
df.dateFormat = @"yyyy-MM-dd";
NSArray *temp=[[NSString stringWithFormat:@"%@",[df stringFromDate:DatePicker.date]] componentsSeparatedByString:@""];
[dateString2 release];
dateString2=nil;
dateString2= [[NSString alloc]initWithString:[temp objectAtIndex:0]];
NSLog(@"%@",dateString2);
Estoy recibiendo el 2012-10-30, pero quiero 30/10/2012.
Sólo para ver cómo termina:
NSDateFormatter* df = [[[NSDateFormatter alloc]init] autorelease];
df.dateFormat = @"MM/dd/yyyy";
NSString* dateString = [df stringFromDate:datePicker.date]; // Don''t use leading caps on variables
NSLog(@"%@", dateString);
Uso un código con las opciones NSISO8601DateFormatWithDashSeparatorInDate:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString * stringformat = [NSDateFormatter dateFormatFromTemplate:@"MMddy" options:NSISO8601DateFormatWithDashSeparatorInDate locale:[NSLocale systemLocale]];
[dateFormatter setDateFormat:stringformat];
NSDate *d = [NSDate date]; // or set your date
NSLog(@"date in format %@", [dateFormatter stringFromDate:d]); // date in format 2018-06-20
Ver instrucciones o introducción con cada línea.
NSString *str = @"2012-10-30"; /// here this is your date with format yyyy-MM-dd
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:@"yyyy-MM-dd"]; //// here set format of date which is in your output date (means above str with format)
NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];// here set format which you want...
NSString *convertedString = [dateFormatter stringFromDate:date]; //here convert date in NSString
NSLog(@"Converted String : %@",convertedString);
df.dateFormat = @ "MM / dd / aaaa";
- (void)showTime {
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"HH:mm:ss"];
// you label
_time.text = [dateFormatter stringFromDate:now];
}
- (void)showDate {
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
// you label
_date.text = [dateFormatter stringFromDate:now];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.showTime;
self.showDate;
}
Disfrutar
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy"];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MM/dd/yyyy"];
NSString *str = @"2012-10-30"; /// here this is your date with format yyyy-MM-dd
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:@"yyyy-MM-dd"]; //// here set format of date which is in your output date (means above str with format)
NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];// here set format which you want...
NSString *convertedString = [dateFormatter stringFromDate:date]; here convert date in NSString
NSLog(@"Converted String : %@",convertedString);