IPhone development-NSData acquisition in years, months, weeks, days, hours, minutes, seconds, and milliseconds and NSDataToNSString Method

Source: Internet
Author: User

Obtain time information in NSDate, in years, months, weeks, days, hours, minutes, seconds, and milliseconds:

Note: The first method cannot obtain information in milliseconds, and the second method is used to obtain information in milliseconds.


[Cpp] NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDate * now;
NSDateComponents * comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
Now = [NSDate date];
Comps = [calendar components: unitFlags fromDate: now];
Int year = [comps year];
Int week = [comps weekday];
Int month = [comps month];
Int day = [comps day];
Int hour = [comps hour];
Int min = [comps minute];
Int sec = [comps second];
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDate * now;
NSDateComponents * comps = [[NSDateComponents alloc] init];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
Now = [NSDate date];
Comps = [calendar components: unitFlags fromDate: now];
Int year = [comps year];
Int week = [comps weekday];
Int month = [comps month];
Int day = [comps day];
Int hour = [comps hour];
Int min = [comps minute];
Int sec = [comps second]; NSDataToNSString method:
[Cpp]-(NSString *) NSDateToNSTring :( NSDate *) nsDate {
// NSString * string = [nsDate descriptionWithCalendarFormat: @ "% Y/% m/% d % H: % M: % S" timeZone: nil locale: nil]; this method has APIs. Do not use this method if you want to upload an APP.
NSDateFormatter * fmt = [[NSDateFormatter alloc] init] autorelease];
-(NSString *) NSDateToNSTring :( NSDate *) nsDate {
// NSString * string = [nsDate descriptionWithCalendarFormat: @ "% Y/% m/% d % H: % M: % S" timeZone: nil locale: nil]; this method has APIs. Do not use this method if you want to upload an APP.
NSDateFormatter * fmt = [[NSDateFormatter alloc] init] autorelease]; [cpp] // [fmt setDateFormat: @ "hh: mm: ss: SSS"];
// [Fmt setDateFormat: @ "hh: mm: ss: SSS"]; [cpp] [fmt setDateFormat: @ "yyyy/MM/dd hh: mm: ss: SSS "];
NSString * string = [fmt stringFromDate: nsDate];
Return string;
[Fmt setDateFormat: @ "yyyy/MM/dd hh: mm: ss: SSS"];
NSString * string = [fmt stringFromDate: nsDate];
Return string;
}

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.