[IOS开发教程] NSDate 日期操作工具类
2015-11-23 15:25
302 查看
//
// main.m
// OC05-task-06
//
// Created by Xin the Great on 15-1-26.
// Copyright (c) 2015年 Xin the Great. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const charchar * argv[]) {
@autoreleasepool {
// insert code here...
//////////////////////NSDate --- 日期//////////////////////
//获取当前系统的时间 标准时间 GMT 格林尼治时间
NSDate *date = [NSDate date];
NSLog(@"date is %@", date);
NSDate *date1 = [[NSDate alloc] init];
NSLog(@"date1 is %@", date1);
//获取时间戳 以秒为单位
NSTimeInterval time1970 = [date timeIntervalSince1970];
NSLog(@"time1970 is %.1f", time1970);
NSTimeInterval time2001 = [date timeIntervalSinceReferenceDate];
NSLog(@"time2001 is %.1f", time2001);
NSTimeInterval time = [date timeIntervalSinceNow];
NSLog(@"time is %.1f", time);
//获取昨天的时间
NSTimeInterval second = 224 * 660 * 60;
NSDate *yesterDayDate = [[NSDate alloc] initWithTimeIntervalSinceNow:-second];
NSLog(@"yesterDayDate is %@",yesterDayDate);
//获取明天的时间
NSDate *tomorrowDayDate = [NSDate dateWithTimeInterval:second sinceDate:[NSDate date]];
NSLog(@"tomorrowDayDate is %@", tomorrowDayDate);
//获得未来的某一个时间
NSDate *future = [NSDate distantFuture];
NSLog(@"future is %@", future);
//获得古代的某一个时间
NSDate *past = [NSDate distantPast];
NSLog(@"past is %@", past);
//日期的比较
// BOOL isTure = [date isEqualToDate:date1];
// NSLog(@"isTure is %d", isTure);
//返回两个时间比较早的那个时间
NSDate *earlierDate = [tomorrowDayDate earlierDate:future];
NSLog(@"earlierDate is %@", earlierDate);
//返回两个时间比较晚的那个时间
NSDate *later = [tomorrowDayDate laterDate:future];
NSLog(@"later is %@", later);
//将时间戳转换成字符串
NSString *str = @"123456789";
NSTimeInterval second2 = [str doubleValue];
NSDate *date3 = [NSDate dateWithTimeIntervalSince1970:second2];
NSLog(@"date3 is %@", date3);
//格式化日期类
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy年MM月dd日 HH小时mm分钟ss秒 ZZZZ"];
//将日期按照格式化日期类转换为字符串
NSString *str2 = [df stringFromDate:date3];
NSLog(@"str2 is %@", str2);
//通过字符串转换为date
NSDate *date4 = [df dateFromString:str2];
NSLog(@"date4 is %@", date4);
}
return 0;
}
// main.m
// OC05-task-06
//
// Created by Xin the Great on 15-1-26.
http://www.kmnk01.com/hxpfk/2015/dyxf_1123/86.html |
//
#import <Foundation/Foundation.h>
int main(int argc, const charchar * argv[]) {
@autoreleasepool {
// insert code here...
//////////////////////NSDate --- 日期//////////////////////
//获取当前系统的时间 标准时间 GMT 格林尼治时间
NSDate *date = [NSDate date];
NSLog(@"date is %@", date);
http://www.kmnk01.com/hxpfk/2015/zf_1123/87.html |
NSLog(@"date1 is %@", date1);
//获取时间戳 以秒为单位
NSTimeInterval time1970 = [date timeIntervalSince1970];
NSLog(@"time1970 is %.1f", time1970);
http://www.kmnk01.com/hxpfk/2015/hzj_1123/94.html |
NSLog(@"time2001 is %.1f", time2001);
NSTimeInterval time = [date timeIntervalSinceNow];
NSLog(@"time is %.1f", time);
http://www.kmnk01.com/hxpfk/2015/gm_1123/93.html |
NSTimeInterval second = 224 * 660 * 60;
NSDate *yesterDayDate = [[NSDate alloc] initWithTimeIntervalSinceNow:-second];
NSLog(@"yesterDayDate is %@",yesterDayDate);
//获取明天的时间
NSDate *tomorrowDayDate = [NSDate dateWithTimeInterval:second sinceDate:[NSDate date]];
NSLog(@"tomorrowDayDate is %@", tomorrowDayDate);
//获得未来的某一个时间
http://www.kmnk01.com/hxpfk/2015/bdy_1123/88.html |
NSLog(@"future is %@", future);
//获得古代的某一个时间
NSDate *past = [NSDate distantPast];
NSLog(@"past is %@", past);
http://www.kmnk01.com/hxpfk/2015/pfsy_1123/89.html |
// BOOL isTure = [date isEqualToDate:date1];
// NSLog(@"isTure is %d", isTure);
//返回两个时间比较早的那个时间
NSDate *earlierDate = [tomorrowDayDate earlierDate:future];
http://www.kmnk01.com/hxpfk/2015/bt_1123/92.html |
//返回两个时间比较晚的那个时间
NSDate *later = [tomorrowDayDate laterDate:future];
NSLog(@"later is %@", later);
//将时间戳转换成字符串
NSString *str = @"123456789";
NSTimeInterval second2 = [str doubleValue];
NSDate *date3 = [NSDate dateWithTimeIntervalSince1970:second2];
http://www.kmnk01.com/hxpfk/2015/bt_1123/90.html |
//格式化日期类
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy年MM月dd日 HH小时mm分钟ss秒 ZZZZ"];
http://www.kmnk01.com/hxpfk/2015/bt_1123/91.html |
NSString *str2 = [df stringFromDate:date3];
NSLog(@"str2 is %@", str2);
//通过字符串转换为date
NSDate *date4 = [df dateFromString:str2];
NSLog(@"date4 is %@", date4);
http://www.kmnk01.com/hxpfk/2015/hzj_1123/95.html |
return 0;
}
相关文章推荐
- iOS 9音频应用播放音频之播放控制暂停停止前进后退的设置
- iOS 9音频应用播放音频之播放控制暂停停止前进后退的设置
- fir.im Weekly - 17 个提升 iOS 开发效率的必备工具
- fir.im Weekly - 17 个提升 iOS 开发效率的必备工具
- iOS SDK详解之Runloop
- KVO
- iOS Cocoapods导入第三方头文件没提示
- iOS程序国际化的使用
- iOS crash分析
- 芒果iOS开发之OC类继承图
- ios添加真机调试的步骤
- iOS企业版程序发布--升级
- iOS中判断设备系统版本
- 内存泄漏检查
- iOS9系列专题一——3D Touch
- iOS 【应用程序原理-3 importantFiles(Info.plist、InfoPlist.strings、.pch文件/自定义NSLog宏)】
- IOS7原生API进行二维码条形码的扫描
- ios蓝牙开发(二)ios连接外设的代码实现
- iOS Line drawing tool(画线类, 直线和 折线)
- ios蓝牙开发(一)蓝牙相关基础知识