NSDate
// Obtain the current date
NSDate * date = [NSDate date];
NSLog (@ "date: % @", date );
// Obtain the date (24*60*60) before 24 hours. dateWithTimeIntervalSinceNow:
NSDate * yesterday = [NSDate dateWithTimeIntervalSinceNow:-(24*60*60)];
NSLog (@ "yesterday: % @", yesterday );
NSDateFormatter * formatter = [[NSDateFormatter alloc] init] autorelease];
NSDate * date = [NSDate date];
[Formatter setTimeStyle: NSDateFormatterMediumStyle];
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease];
NSDateComponents * comps = [[NSDateComponents alloc] init] autorelease];
NSInteger unitFlags = NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit |
NSWeekdayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit;
// Int week = 0;
Comps = [calendar components: unitFlags fromDate: date];
Int week = [comps weekday];
Int year = [comps year];
Int month = [comps month];
Int day = [comps day];
// [Formatter setDateStyle: NSDateFormatterMediumStyle];
// This sets the label with the updated time.
Int hour = [comps hour];
Int min = [comps minute];
Int sec = [comps second];
NSLog (@ "week % d", week );
NSLog (@ "year % d", year );
NSLog (@ "month % d", month );
NSLog (@ "day % d", day );
NSLog (@ "hour % d", hour );
NSLog (@ "min % d", min );
NSLog (@ "sec % d", sec );
// Get the millisecond
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[DateFormatter setDateStyle: NSDateFormatterMediumStyle];
[DateFormatter setTimeStyle: nsdateformatter1_style];
// [DateFormatter setDateFormat: @ "hh: mm: ss"]
[DateFormatter setDateFormat: @ "yyyy-MM-dd HH: mm: ss. SSS"];
NSLog (@ "Date % @", [dateFormatter stringFromDate: [NSDate date]);
[DateFormatter release];
========================================================== =
Method 1
NSDate * toDate1 = [[NSDate alloc] initWithString: @ "2520-9-26 17:10:00 + 0600"];
NSTimeInterval distance = [toDate1 timeIntervalSinceNow];
NSTimeInterval iDat = distance/(86400 );
NSLog (@ "From now to % @ diff: % f", [toDate1 description], iDat );
[ToDate1 release];
Method 2
NSDate * toDate = [[NSDate alloc] initWithString: @ "2009-9-29 + 0600"];
NSDate * startDate = [[NSDate alloc] init];
NSCalendar * chineseClendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit |
NSSecondCalendarUnit | NSDayCalendarUnit
| NSMonthCalendarUnit | NSYearCalendarUnit;
NSDateComponents * cps = [chineseClendar components: unitFlags fromDate: startDate toDate: toDate options: 0];
NSInteger diffHour = [cps hour];
NSInteger diffMin = [cps minute];
NSInteger diffSec = [cps second];
NSInteger diffDay = [cps day];
NSInteger diffMon = [cps month];
NSInteger diffYear = [cps year];
NSLog (@ "From Now to % @, diff: Years: % d Months: % d, Days; % d, Hours: % d, Mins: % d, sec: % d ",
[ToDate description], diffYear, diffMon, diffDay, diffHour, diffMin, diffSec );
[ToDate release];
[StartDate release];
[ChineseClendar release];
NSDate
// Obtain the current date www.2cto.com
NSDate * date = [NSDate date];
NSLog (@ "date: % @", date );
// Obtain the date (24*60*60) before 24 hours. dateWithTimeIntervalSinceNow:
NSDate * yesterday = [NSDate dateWithTimeIntervalSinceNow:-(24*60*60)];
NSLog (@ "yesterday: % @", yesterday );
NSDateFormatter * formatter = [[NSDateFormatter alloc] init] autorelease];
NSDate * date = [NSDate date];
[Formatter setTimeStyle: NSDateFormatterMediumStyle];
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar] autorelease];
NSDateComponents * comps = [[NSDateComponents alloc] init] autorelease];
NSInteger unitFlags = NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit |
NSWeekdayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit;
// Int week = 0;
Comps = [calendar components: unitFlags fromDate: date];
Int week = [comps weekday];
Int year = [comps year];
Int month = [comps month];
Int day = [comps day];
// [Formatter setDateStyle: NSDateFormatterMediumStyle];
// This sets the label with the updated time.
Int hour = [comps hour];
Int min = [comps minute];
Int sec = [comps second];
NSLog (@ "week % d", week );
NSLog (@ "year % d", year );
NSLog (@ "month % d", month );
NSLog (@ "day % d", day );
NSLog (@ "hour % d", hour );
NSLog (@ "min % d", min );
NSLog (@ "sec % d", sec );
// Get the millisecond
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[DateFormatter setDateStyle: NSDateFormatterMediumStyle];
[DateFormatter setTimeStyle: nsdateformatter1_style];
// [DateFormatter setDateFormat: @ "hh: mm: ss"]
[DateFormatter setDateFormat: @ "yyyy-MM-dd HH: mm: ss. SSS"];
NSLog (@ "Date % @", [dateFormatter stringFromDate: [NSDate date]);
[DateFormatter release];
========================================================== =
Method 1
NSDate * toDate1 = [[NSDate alloc] initWithString: @ "2520-9-26 17:10:00 + 0600"];
NSTimeInterval distance = [toDate1 timeIntervalSinceNow];
NSTimeInterval iDat = distance/(86400 );
NSLog (@ "From now to % @ diff: % f", [toDate1 description], iDat );
[ToDate1 release];
Method 2
NSDate * toDate = [[NSDate alloc] initWithString: @ "2009-9-29 + 0600"];
NSDate * startDate = [[NSDate alloc] init];
NSCalendar * chineseClendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit |
NSSecondCalendarUnit | NSDayCalendarUnit
| NSMonthCalendarUnit | NSYearCalendarUnit;
NSDateComponents * cps = [chineseClendar components: unitFlags fromDate: startDate toDate: toDate options: 0];
NSInteger diffHour = [cps hour];
NSInteger diffMin = [cps minute];
NSInteger diffSec = [cps second];
NSInteger diffDay = [cps day];
NSInteger diffMon = [cps month];
NSInteger diffYear = [cps year];
NSLog (@ "From Now to % @, diff: Years: % d Months: % d, Days; % d, Hours: % d, Mins: % d, sec: % d ",
[ToDate description], diffYear, diffMon, diffDay, diffHour, diffMin, diffSec );
[ToDate release];
[StartDate release];
[ChineseClendar release];
From the column zcl316369