Foundation框架 - NSDate类

NSDate类

//
//  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 时间间隔

        //获取当前日期的两种方式:
        //NSDate* date =[[NSDate alloc]init];
        NSLog(@"******************** NSTimeInterval **********************");
        //@property(readonly) NSTimeInterval timeIntervalSinceNow

        NSDate* date = [NSDate date];
        NSLog(@"当前时间为:%@",date);

        //当前时间到现在的时间间隔  (理论值为0)
        NSTimeInterval  dateIntvl=[date timeIntervalSinceNow];
        NSLog(@"当前时间到现在的时间为:%f",dateIntvl);

        //从1970年到现在的时间间隔
        dateIntvl=[date timeIntervalSince1970];
        NSLog(@"从1970年到现在的时间为:%f",dateIntvl);

        //从苹果的参考时间到现在的时间间隔
        dateIntvl=[date timeIntervalSinceReferenceDate];
        NSLog(@"从苹果参考时间到现在的时间为:%fu",dateIntvl);

        //从现在开始的2天后的时间
        NSTimeInterval nextIntvl=2*24*60*60;
        NSDate* next=[NSDate dateWithTimeIntervalSinceNow:nextIntvl];
        NSLog(@"从现在开始2天后的时间为:%@",next);

NSDateFormatter 日期格式化

       NSLog(@"*********** NSDateFormatter 日期格式化*********************");

        NSDate* curDate=[NSDate new];
        NSDateFormatter* fmt=[[NSDateFormatter alloc]init];

        //dateStyle 系统日期格式
        fmt.dateStyle=NSDateFormatterShortStyle;    //14-12-26
        fmt.dateStyle=NSDateFormatterLongStyle;     //2014年12月26日
        fmt.dateStyle=NSDateFormatterFullStyle;     //2014年12月26日 星期五

        NSString* fmtStr=[fmt stringFromDate:curDate];
        NSLog(@"格式化后的日期为:%@",fmtStr);

        NSDateFormatter* fmt1=[[NSDateFormatter alloc]init];

        //dateFormat 自定义日期格式
        fmt1.dateFormat[email protected]"yyyy/MM/dd HH:mm";               //2014/12/26 11:57
        fmt1.dateFormat[email protected]"yyyy-MM-dd hh:mm:ss";            //2014-12-26 11:57:30
        fmt1.dateFormat[email protected]"yyyy年MM月dd日 hh时mm分ss秒SSSSS"; //2014y12m26日 12时01分52秒15700
        fmt1.dateFormat[email protected]"yy.MM.dd hh.mm.ss";
        NSString* fmtStr1=[fmt1 stringFromDate:curDate];
        NSLog(@"格式化后的日期为:%@",fmtStr1);

        //将字符串格式转换为日期格式
        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];

        //输出时间戳
        NSTimeInterval intvl=[dt timeIntervalSinceReferenceDate];
        NSLog(@"转换后的时间为:%@,时间戳为:%f",dt,intvl);

NSTimeZone 时区转换

         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");
//时区转换
static void transTimeZone(NSString* currDateStr)
{
    NSDate* currDate=nil;
    NSLog(@"北京时间:%@",currDateStr);
    NSDateFormatter *fmt3=[NSDateFormatter new];
    fmt3.dateFormat[email protected]"yyyy-MM-dd HH:mm:ss";
    currDate =[fmt3 dateFromString:currDateStr];

    //北京时间转为东京时间
    fmt3.timeZone=[NSTimeZone timeZoneWithName:@"Asia/Tokyo"];
    NSString* zoneTransed=[fmt3 stringFromDate:currDate];
    NSLog(@"东京时间:%@",zoneTransed);

    //北京时间转为纽约时间ff
    fmt3.timeZone=[NSTimeZone timeZoneWithName:@"Europe/Moscow"];
    NSString* zoneTransed1=[fmt3 stringFromDate:currDate];
    NSLog(@"墨西哥时间:%@",zoneTransed1);

    //北京时间转换为纽约时间
    fmt3.timeZone=[NSTimeZone timeZoneWithName:@"America/New_York"];
    NSString* zoneTransed2=[fmt3 stringFromDate:currDate];
    NSLog(@"纽约时间:%@",zoneTransed2);

}

NSCalendar 日历与日期

 NSLog(@"****************** NSCalendar 日历与日期*******************");

        NSString* [email protected]"2014-2-14";
        NSDateFormatter* df2=[NSDateFormatter new];
        df2.dateFormat[email protected]"yyyy-MM-dd";
        //求当月最后一天是几号?(求这个月有多少天)
        NSCalendar* cal=[NSCalendar currentCalendar];
        NSDate* date1 =[df2 dateFromString:source1];

        // smaller =天(NSCalendarUnitDay)
        // larger  =月 (NSCalendarUnitMonth)
        NSRange rng=[cal rangeOfUnit:NSCalendarUnitDay
                              inUnit:NSCalendarUnitMonth forDate:date1];
        NSLog(@"%@这个月共有%ld天",source1,rng.length);

        //设置 每周的第一天 1=sunday 2=monday
        [cal setFirstWeekday:2];

        //求本周是星期几?
        NSUInteger day=[cal ordinalityOfUnit:NSCalendarUnitDay
                                      inUnit:NSWeekCalendarUnit forDate:date1];
        NSLog(@"今天是星期:%lu",day);

NSDateComponents 获得日期的各项值

NSLog(@"*********** NSDateComponents 获得日期的各项值 *************");

        //通过NSDateComponents获得日期的各项值 (| "或")
        NSCalendarUnit units=NSCalendarUnitDay|NSCalendarUnitMonth
        |NSCalendarUnitWeekday|NSCalendarUnitYear;

        NSDateComponents* ncp=[cal components:units fromDate:date1];
        NSLog(@"输出ncp:%@",ncp.description);

        //求下个月的这一天是星期几?
        ncp.month +=1;
        NSDate* newDate =[cal dateFromComponents:ncp];

        NSUInteger newday=[cal ordinalityOfUnit:NSCalendarUnitDay
                                         inUnit:NSWeekCalendarUnit
                                        forDate:newDate];
        NSLog(@"下个月的今天[%@]是星期:%lu",[df stringFromDate:newDate],newday);

    }
时间: 2024-08-06 01:22:32

Foundation框架 - NSDate类的相关文章

黑马程序员--Foundation框架之--类其他常用的类:NSNumber,NSDate

------IOS培训期待与您交流! ------- Foundation的其他常用的类:NSNumber,NSDate 一.NSNumber 这个类主要是用来封装基本类型的,OC 中的集合是不允许存入基本类型的,所以NSNumber类就诞生了,需要将基本类型封装一下,然后存进去. 从这一点上我们就可以看出NSNumber是很重要的,后面一定会经常用到. 1.封包和解包 // 创建NSNumber // 封包 NSNumber *intNumber = [NSNumber numberWithI

黑马程序员——Foundation框架常用类(NSDate)

------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------ 简介 NSDate:用于保存时间值的一个OC类,同时提供了一些方法来处理一些与时间相关的事.NSDate对象用来表示一个具体的时间点.NSDate是一个类簇,我们所使用的NSDate对象,都是NSDate的私有子类的实体.NSDate存储的是GMT时间,使用的时候会根据 当前应用 指定的 时区 进行时间上的增减,以供计算或显示. NSDate 1.时间对象的基本使用 //第一种创建方式(里面存储

OC — (Foundation框架-NSDate)

NSDate:是OC中处理日期时间的一个类,可以用来表示时间 获取当前的时间 NSDate *d = [NSDate date]; 创建日期时间对象 NSLog输出是当前时间(东八时区) 格式化显示时间 NSDate *d1 = [NSDate date]; NSLog(@"%@", d1); // 格式化日期,时间 // NSDateFormatter 日期格式化 /* yyyy 表示四位的年份 MM 表示2位的月份 dd 表示2位的天数 HH 表示24小时制的小时 hh 12小时制

Foundation框架 - NSNumber类

NSNumber类 NSFormatter #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSLog(@"1******************* NSFormatter ***********************"); NSNumber* nb1=[NSNumber numberWithInt:25]; NSNumber* n

Foundation框架 - NSException类

NSException类 WBStudentManager.h #import <Foundation/Foundation.h> NSString* const NameInvalidException ; @interface WBStudentManager : NSObject - (void)registStudent:(NSString*)name; @end WBStudentManager.m #import "WBStudentManager.h" @im

Foundation框架 - NSSet类 、NSMutableSet类

NSSet和NSArray的对比 /* NSSet和NSArray的对比 1> 共同点 * 都是集合,都能存放多个OC对象 * 只能存放OC对象,不能存放非OC对象类型(基本数据类型:int.char.float等,结构体,枚举) * 本身都不可变,都有一个可变的子类 2> 不同点 * NSArray有顺序,NSSet没有顺序 */ 重点内容 #import <Foundation/Foundation.h> #import "Person.h" static

Foundation框架 - NSDictionary类、NSMutableDictionary类

NSArray.NSSet.NSDictionary /* 集合 1.NSArray\NSMutableArray * 有序 * 高速创建(不可变):@[obj1, obj2, obj3] * 高速訪问元素:数组名[i] 2.NSSet\NSMutableSet * 无序 3.NSDictionary\NSMutableDictionary * 无序 * 高速创建(不可变):@{key1 : value1, key2 : value2} * 高速訪问元素:字典名[key] */ 字典创建 #im

iOS Foundation 框架基类

太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的漂亮人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 基类 Title Topic Date NSObject Class Reference Data Types & Collections

黑马程序员——Foundation框架常用类(NSNumber/NSValue)

------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------ 简介 引入:NSArray.NSSet.NSDictionary这些OC集合都只能存放OC对象,不能存放基本数据类型(int.char.double...).如果我们想将基本数据类型存到这些OC集合中,那么我们将基本数据类型(int.char.double...)包装成OC对象,那么就可以存到OC集合中.NSNumber可以将基本数据类型包装为OC对象. NSValue:用来存储一个C或者OC的