betoesquivel

Usando NSDate y NSDateFormatter

Jan 26th, 2015
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ViewController.m
  3. //  Fechass
  4. //
  5. //  Created by alumno on 1/26/15.
  6. //  Copyright (c) 2015 ITESM. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10.  
  11. @interface ViewController ()
  12.  
  13. @end
  14.  
  15. @implementation ViewController
  16.  
  17. - (void)viewDidLoad {
  18.     [super viewDidLoad];
  19.     // Do any additional setup after loading the view, typically from a nib.
  20.     NSDate *fechaHoy;
  21.     NSDateFormatter *formatoFecha;
  22.    
  23.     fechaHoy = [[NSDate alloc] init];
  24.     formatoFecha = [[NSDateFormatter alloc] init];
  25.    
  26.     // Tue, 21 Aug
  27.     [formatoFecha setDateFormat:@"EE, dd MMM"];
  28.     NSString *fechaAMostrar = [formatoFecha stringFromDate:fechaHoy];
  29.     NSLog(@"\n%@", fechaAMostrar);
  30.    
  31.     // Tue 21 August
  32.     [formatoFecha setDateFormat:@"EE dd MMMM"];
  33.     fechaAMostrar = [formatoFecha stringFromDate:fechaHoy];
  34.     NSLog(@"\n%@", fechaAMostrar);
  35.    
  36.     // 21/08/2012 --- 22:39:02
  37.     [formatoFecha setDateFormat:@"dd/MM/yyyy --- HH:mm:ss"];
  38.     fechaAMostrar = [formatoFecha stringFromDate:fechaHoy];
  39.     NSLog(@"\n%@", fechaAMostrar);
  40.    
  41.     // 21/Aug/2012 --- 10:39:02 PM
  42.     [formatoFecha setDateFormat:@"dd/MMM/yyyy --- hh:mm:ss a"];
  43.     fechaAMostrar = [formatoFecha stringFromDate:fechaHoy];
  44.     NSLog(@"\n%@", fechaAMostrar);
  45.    
  46.     // despues de las 10 PM
  47.     [formatoFecha setDateFormat:@"'despues de las ' hh a"];
  48.     fechaAMostrar = [formatoFecha stringFromDate:fechaHoy];
  49.     NSLog(@"\n%@", fechaAMostrar);
  50.    
  51.     // Dias transcurridos en el anio 234
  52.     [formatoFecha setDateFormat:@"'Dias transcurridos en el anio' DDD"];
  53.     fechaAMostrar = [formatoFecha stringFromDate:fechaHoy];
  54.     NSLog(@"\n%@", fechaAMostrar);
  55.    
  56.     NSDate *fechaNueva = [fechaHoy dateByAddingTimeInterval:604800];
  57.     // Semana siguiente: Tue 28 August
  58.     [formatoFecha setDateFormat:@"'Semana siguiente: 'EE dd MMMM"];
  59.     fechaAMostrar = [formatoFecha stringFromDate:fechaNueva];
  60.     NSLog(@"\n%@", fechaAMostrar);
  61.    
  62.     fechaNueva = [fechaHoy dateByAddingTimeInterval:-604800];
  63.     // Semana anterior: Tue 14 August
  64.     [formatoFecha setDateFormat:@"'Semana anterior: 'EE dd MMMM"];
  65.     fechaAMostrar = [formatoFecha stringFromDate:fechaNueva];
  66.     NSLog(@"\n%@", fechaAMostrar);
  67.    
  68.     fechaNueva = [fechaHoy dateByAddingTimeInterval:86400];
  69.     // Dia siguiente: Wed 22 August
  70.     [formatoFecha setDateFormat:@"'Dia siguiente: 'EE dd MMMM"];
  71.     fechaAMostrar = [formatoFecha stringFromDate:fechaNueva];
  72.     NSLog(@"\n%@", fechaAMostrar);
  73.    
  74.     fechaNueva = [fechaHoy dateByAddingTimeInterval:-86400];
  75.     // Dia anterior: Mon 20 August
  76.     [formatoFecha setDateFormat:@"'Dia anterior: 'EE dd MMMM"];
  77.     fechaAMostrar = [formatoFecha stringFromDate:fechaNueva];
  78.     NSLog(@"\n%@", fechaAMostrar);
  79. }
  80.  
  81. - (void)didReceiveMemoryWarning {
  82.     [super didReceiveMemoryWarning];
  83.     // Dispose of any resources that can be recreated.
  84. }
  85.  
  86. @end
Advertisement
Add Comment
Please, Sign In to add comment