Foundation framework, foundation

Source: Internet
Author: User
Tags date1

Foundation framework, foundation

NSDate class

//// Main. m // 8. NSDate // Created by wangzhaolu on 14-2-25. // Copyright (c) 2014 Turing. all rights reserved. // # import <Foundation/Foundation. h> static void transTimeZone (NSString * currDateStr); int main (int argc, const char * argv []) {@ autoreleasepool {

NSTimeInterval Interval

// Obtain the current date in two ways: // NSDate * date = [[NSDate alloc] init]; NSLog (@ "******************** NSTimeInterval *************** *******"); // @ property (readonly) NSTimeInterval timeIntervalSinceNow NSDate * date = [NSDate date]; NSLog (@ "Current Time: % @", date ); // The time interval from the current time to the present (the theoretical value is 0) NSTimeInterval dateIntvl = [date timeIntervalSinceNow]; NSLog (@ "the current time to the present time is: % f ", dateIntvl); // the interval from January 1, 1970 to the present dateIntvl = [date timeIntervalSince1970]; NSLog (@ "from January 1, 1970 to the present time: % f", dateIntvl ); // The time interval from the apple reference time to the present dateIntvl = [date timeIntervalSinceReferenceDate]; NSLog (@ "from the apple reference time to the present time: % fu", dateIntvl ); // NSTimeInterval nextIntvl = 2*24*60*60; NSDate * next = [NSDate dateWithTimeIntervalSinceNow: nextIntvl]; NSLog (@ "Two days from now on: % @", next );

NSDateFormatter date formatter

NSLog (@ "************ NSDateFormatter date formatting *********************" ); NSDate * curDate = [NSDate new]; NSDateFormatter * fmt = [[NSDateFormatter alloc] init]; // dateStyle system Date Format fmt. dateStyle = nsdateformatter1_style; // 14-12-26 fmt. dateStyle = NSDateFormatterLongStyle; // February December 26, 2014 fmt. dateStyle = NSDateFormatterFullStyle; // Friday, January 1, December 26, 2014 NSString * fmtStr = [fmt stringFromDate: curDate]; NSLog (@ "formatted Date: % @", fmtStr ); NSDateFormatter * fmt1 = [[NSDateFormatter alloc] init]; // dateFormat custom Date Format fmt1.dateFormat = @ "yyyy/MM/dd HH: mm "; // 2014/12/26 11: 57 fmt1.dateFormat = @ "yyyy-MM-dd hh: mm: ss "; // 11:57:30 fmt1.dateFormat = @ "MM dd, yyyy, hh, mm, ss, second, SSSSS"; // February 26, 15700, fmt1.dateFormat = @ "yy. MM. dd hh. mm. ss "; NSString * fmtStr1 = [fmt1 stringFromDate: curDate]; NSLog (@" formatted Date: % @ ", fmtStr1 ); // convert the string format to the date format NSString * source = @ "1980-12-15"; NSDateFormatter * df = [NSDateFormatter new]; NSString * dateFmt = @ "yyyy-MM-dd "; df. dateFormat = dateFmt; df. timeZone = [NSTimeZone timeZoneWithName: @ "Europe/London"]; NSDate * dt = [df dateFromString: source]; // output timestamp NSTimeInterval intvl = [dt timeIntervalSinceReferenceDate]; NSLog (@ "converted time: % @, timestamp: % f", dt, intvl );

NSTimeZone time zone conversion

NSLog (@ "******************* NSTimeZone time zone conversion *************** *****"); // traverse the time zone object NSTimeZone NSArray * zoneNames = [NSTimeZone knownTimeZoneNames]; for (int I = 0; I <zoneNames. count; I ++) {NSLog (@ "% @", zoneNames [I]);} // call the time zone conversion function transTimeZone (@ "20:08:08 ");
// Time zone conversion static void transTimeZone (NSString * currDateStr) {NSDate * currDate = nil; NSLog (@ "Beijing Time: % @", currDateStr ); NSDateFormatter * fmt3 = [NSDateFormatter new]; fmt3.dateFormat = @ "yyyy-MM-dd HH: mm: ss"; currDate = [fmt3 dateFromString: currDateStr]; // Beijing time to Tokyo time fmt3.timeZone = [NSTimeZone timeZoneWithName: @ "Asia/Tokyo"]; NSString * zoneTransed = [fmt3 stringFromDate: currDate]; NSLog (@ "Tokyo time: % @ ", zoneTransed); // convert Beijing Time To New York time ff fmt3.timeZone = [NSTimeZone region: @" Europe/Moscow "]; NSString * zoneTransed1 = [fmt3 stringFromDate: currDate]; NSLog (@ "Mexico time: % @", zoneTransed1); // Beijing time is converted to New York time fmt3.timeZone = [NSTimeZone timeZoneWithName: @ "America/New_York"]; NSString * zoneTransed2 = [fmt3 stringFromDate: currDate]; NSLog (@ "New York time: % @", zoneTransed2 );}

NSCalendar calendar and date

NSLog (@ "******************* NSCalendar calendar and date ************** *****"); NSString * source1 = @ "2014-2-14"; NSDateFormatter * df2 = [NSDateFormatter new]; df2.dateFormat = @ "yyyy-MM-dd"; // Number of the last day of the current month? (How many days are there in this month?) NSCalendar * cal = [NSCalendar currentCalendar]; NSDate * date1 = [df2 dateFromString: source1]; // smaller = Day (NSCalendarUnitDay) // larger = month (NSCalendarUnitMonth) nsange rng = [cal rangeOfUnit: NSCalendarUnitDay inUnit: NSCalendarUnitMonth forDate: date1]; NSLog (@ "% @ this month Total % ld days", source1, rng. length); // set the first day of each week. 1 = Sunday 2 = Monday [cal setFirstWeekday: 2]; // calculate the day of the week? NSUInteger day = [cal ordinalityOfUnit: NSCalendarUnitDay inUnit: NSWeekCalendarUnit forDate: date1]; NSLog (@ "Today is day: % lu", day );

NSDateComponents

NSLog (@ "************ NSDateComponents to obtain various date values *************"); // obtain the date values (| "or") NSCalendarUnit units = hour | NSCalendarUnitMonth | hour | NSCalendarUnitYear; NSDateComponents * ncp = [cal components: units fromDate: date1] Through NSDateComponents; NSLog (@ "output ncp: % @", ncp. description); // What is the day of the week for the next month? Ncp. month + = 1; NSDate * newDate = [cal dateFromComponents: ncp]; NSUInteger newday = [cal ordinalityOfUnit: NSCalendarUnitDay inUnit: NSWeekCalendarUnit forDate: newDate]; NSLog (@ "today of the next month [% @] is week: % lu", [df stringFromDate: newDate], newday );}

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.