IPhone development-NSDate operation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.