iOS NSDateFormatter and Compare Two Date

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

[dateFormat setDateFormat:@"yyyy/MM/dd"];
NSDate *date = [dateFormat dateFromString:@"2013/5/23"];

NSString *selectionString = [dateFormat stringFromDate:date];
SO Link(http://stackoverflow.com/questions/12721553/get-time-from-nsdate-returns-nil/12721934#12721934)

Imp Link (http://ios.eezytutorials.com/)

The most commonly used date format specifiers are (keep in mind that they are case sensitive):


  • y = year
  • Q = quarter
  • M = month
  • w = week of year
  • W = week of month
  • d = day of the month
  • D = day of year
  • E = day of week
  • a = period (AM or PM)
  • h = hour (1-12)
  • H = hour (0-23)
  • m = minute
  • s = second


In general, the number of characters in a specifier determine the size of date field. Let’s use an example to illustrate date formatting.
eg. Input date = 2011-05-01 Sunday
1-character = 1-digit/character number or word (if number/word can’t be 1 character long then abbreviation or fullname is displayed).
[dateFormatter setDateFormat:@"E, d M y"];  // Output: Sun, 1 5 2011
2-character = 2-digit/character number or word (if number/word can’t be 2 character long then abbreviation is displayed).
[dateFormatter setDateFormat:@"EE, dd MM yy"];  // Output: Sun, 01 05 11
3-character = 3-digit/character number or word, or abbreviation (generally).
[dateFormatter setDateFormat:@"EEE, ddd MMM yyy"];  // Output: Sun, 001 May 2011
4-character = full name (generally).
[dateFormatter setDateFormat:@"EEEE, dddd MMMM yyyy"];  // Output: Sunday, 0001 May 2011
Here’s the weird part though, if you specify 5 E’s, you get an rather unexpected output. You would think that the output date field would be longer than 1 character:
[dateFormatter setDateFormat:@"EEEEE, ddddd MMMMM yyyyy"];  // Output: S, 00001 M 2011



Compare Two Date



NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
    NSDate *serDate;

    NSDate *endDate;

    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    serDate = [formatter dateFromString:@"2012-12-07 7:17:58"];

    endDate = [formatter dateFromString:@"2012-12-07 08:43:30"];
    NSTimeInterval timeDifference = [endDate timeIntervalSinceDate:serDate];

    double minutes = timeDifference / 60;

    double hours = minutes / 60;

    double seconds = timeDifference;

    double days = minutes / 1440;
    NSLog(@" days = %.0f,hours = %.2f, minutes = %.0f,seconds = %.0f", days, hours, minutes, seconds);


    if (hours > 1.30)
        return 1;
    else
        return 0;




Comments

  1. Best Way TO get Year ,Date Time And etc.......Your contents are too simple to read and easy to understand.

    -------------------------------------------------------------------------------------------------
    iPhone 5 App Development & iPad Application Developer

    ReplyDelete
  2. You are doing date calculation TERRIBLY wrong. Your code doesn't take things like daylight saving times, leap seconds or anything, really, into account. Use the appropriate methods and classes, that is, NSCalendar and NSDateComponents for these kinds of things. Time calculation _is_ extremely hard and like I said, you are doing it wrong.

    ReplyDelete

Post a Comment

Popular posts from this blog

Hacker Rank problem solution

How to Make REST api call in Objective-C.

Building and Running Python Scripts with Xcode