Foundation Framework-NSDate class

Source: Internet
Author: User

NSDate class

////  main.m//  8.NSDate////  Created by wangzhaolu on 14-2-25.//  Copyright (c) 2014年 Turing. All rights reserved.//#import <Foundation/Foundation.h>staticvoid transTimeZone(NSString* currDateStr);int main(intconstchar * argv[]) {    @autoreleasepool {

Nstimeinterval time interval

        //Get the current date in two ways:        //nsdate* date =[[nsdate alloc]init];        NSLog(@"******************** nstimeinterval **********************");//@property (readonly) nstimeinterval Timeintervalsincenow        NSDate* date = [NSDateDate];NSLog(@"Current time:%@", date);//Current time to present time interval (theoretical value is 0)        NstimeintervalDateintvl=[date Timeintervalsincenow];NSLog(@"Current time to Present time:%f", DATEINTVL);//From 1970 to the present time intervalDateintvl=[date timeIntervalSince1970];NSLog(@"From 1970 to the present time:%f", DATEINTVL);//From the Apple reference time to the current time intervalDateintvl=[date Timeintervalsincereferencedate];NSLog(@"from the Apple reference time to the present time:%fu", DATEINTVL);//From now on 2 days after the time        NstimeintervalNextintvl=2* -* -* -;NSDate* next=[NSDateDATEWITHTIMEINTERVALSINCENOW:NEXTINTVL];NSLog(@"2 days from now:%@", next);

NSDateFormatter date Formatting

       NSLog(@"*********** nsdateformatter date formatting *********************");NSDate* curdate=[NSDateNEW]; nsdateformatter* Fmt=[[nsdateformatter Alloc]init];//datestyle System date formatFmt. Datestyle=nsdateformattershortstyle;//14-12-26Fmt. Datestyle=nsdateformatterlongstyle;//December 26, 2014Fmt. Datestyle=nsdateformatterfullstyle;//Friday, December 26, 2014        NSString* Fmtstr=[fmt Stringfromdate:curdate];NSLog(@"Date after formatting:%@", FMTSTR); nsdateformatter* Fmt1=[[nsdateformatter Alloc]init];//dateformat Custom date formatsFmt1. DateFormat[Email protected]"Yyyy/mm/dd hh:mm";//2014/12/26 11:57Fmt1. DateFormat[Email protected]"Yyyy-mm-dd hh:mm:ss";//2014-12-26 11:57:30Fmt1. DateFormat[Email protected]yyyy mm month dd Day hh mm min ss sec sssss;//2014y12m26 Day 12:01 52 sec 15700Fmt1. DateFormat[Email protected]"yy. Mm.dd HH.MM.SS ";NSString* FMTSTR1=[FMT1 Stringfromdate:curdate];NSLog(@"Date after formatting:%@", FMTSTR1);//Convert string format to date format        NSString* [Email protected]"1980-12-15"; nsdateformatter* Df=[nsdateformatter New];NSString* [Email protected]"Yyyy-mm-dd"; Df. DateFormat=DATEFMT; Df. TimeZone=[nstimezone timezonewithname:@"Europe/london"];NSDate* DT=[DF Datefromstring:source];//Output time stamp        NstimeintervalIntvl=[dt Timeintervalsincereferencedate];NSLog(@"The converted time is:%@, timestamp:%f", DT,INTVL);

Nstimezone Time Zone Conversion

         NSLog(@"****************** NSTimeZone 时区转换********************");        //遍历时区对象NSTimeZone        NSArray* zoneNames=[NSTimeZone knownTimeZoneNames];        for (int i=0; i<zoneNames.count; i++) {           NSLog(@"%@",zoneNames[i]);        }        //调用时区转换函数        transTimeZone(@"2008-8-8 20:08:08");
//time zone conversionStatic voidTranstimezone (NSString* Currdatestr) {NSDate* currdate=Nil;NSLog(@"Beijing Time:%@", CURRDATESTR);    NSDateFormatter *fmt3=[nsdateformatter New]; Fmt3. DateFormat[Email protected]"Yyyy-mm-dd HH:mm:ss"; Currdate =[fmt3 DATEFROMSTRING:CURRDATESTR];//Beijing time converted to Tokyo timeFmt3. TimeZone=[nstimezone timezonewithname:@"Asia/tokyo"];NSString* Zonetransed=[fmt3 Stringfromdate:currdate];NSLog(@"Tokyo Time:%@", zonetransed);//Beijing time to New York time FFFmt3. TimeZone=[nstimezone timezonewithname:@"Europe/moscow"];NSString* Zonetransed1=[fmt3 Stringfromdate:currdate];NSLog(@"Mexico Time:%@", zoneTransed1);//Beijing time conversion to New York timeFmt3. 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* [Email protected]"2014-2-14";        nsdateformatter* Df2=[nsdateformatter New]; Df2. DateFormat[Email protected]"Yyyy-mm-dd";//What is the date of the last day of the month? (Ask how many days this month)        Nscalendar* cal=[NscalendarCurrentcalendar];NSDate* Date1 =[DF2 Datefromstring:source1];//smaller = day (nscalendarunitday)        //larger = month (nscalendarunitmonth)        NsrangeRng=[cal rangeofunit:nscalendarunitday Inunit:nscalendarunitmonth fordate:date1];NSLog(@"%@ A total of%ld days this month.", source1,rng. Length);//Set the first day of the week 1=sunday 2=monday[Cal Setfirstweekday:2];//What day of the week is it? Nsuinteger day=[cal ordinalityofunit:nscalendarunitday inunit:nsweekcalendarunit ForD ATE:DATE1];NSLog(@"Today is the week:%lu", day);

Nsdatecomponents the value of the obtained date

NSLog(@"*********** nsdatecomponents to obtain the value of the date *************");//Get the value of the date by nsdatecomponents (| "or")Nscalendarunit units=nscalendarunitday| Nscalendarunitmonth | nscalendarunitweekday|        Nscalendarunityear; nsdatecomponents* ncp=[cal components:units fromdate:date1];NSLog(@"Output ncp:%@"Ncp. Description);//What day of the week is the next month? Ncp. Month+=1;NSDate* Newdate =[cal DATEFROMCOMPONENTS:NCP]; Nsuinteger newday=[cal Ordinalityofunit:nscalendarunitday Inunit:nsweekcalendaruni T fordate:newdate];NSLog(@"next month's Today [%@] is week:%lu", [DF Stringfromdate:newdate],newday); }

Foundation Framework-NSDate class

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.