时间与日期。

1,创建时间。

NSDate *todaysDate = [NSDate date];

NSLog(@"Today‘s date is %@", todaysDate);

后面的+0000很讨厌。

2,由组建创建时间段,比如年月日,十分秒。

timeZoneWithAbbreviation设置时区。

具体代码

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

dateComponents.year = 2007;

dateComponents.month = 6;

dateComponents.day = 29;

dateComponents.hour = 12;

dateComponents.minute = 01;

dateComponents.second = 31;

dateComponents.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];

NSDate *iPhoneReleaseDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];

NSLog(@"The original iPhone went on sale: %@", iPhoneReleaseDate);

3,比较两个时区。

比如看看是不是今天。

NSDate *todaysDate = [NSDate date];

if([todaysDate isEqualToDate:iPhoneReleaseDate])

NSLog(@"The iPhone was released today!");

else

NSLog(@"The iPhone was released on some other date”);

早一天或晚一天。

NSDate *earlierDateIs = [todaysDate earlierDate:iPhoneReleaseDate];

NSDate *laterDateIs = [todaysDate laterDate:iPhoneReleaseDate];

日历

NSCalendar *systemCalendar = [NSCalendar currentCalendar];

unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |

NSDayCalendarUnit;

具体代码

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

dateComponents.year = 2007;

dateComponents.month = 6;

dateComponents.day = 29;

dateComponents.hour = 12;

dateComponents.minute = 01;

dateComponents.second = 31;

dateComponents.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"PDT"];

NSDate *iPhoneReleaseDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];

NSLog(@"The original iPhone went on sale: %@", iPhoneReleaseDate);

NSDate *todaysDate = [NSDate date];

NSLog(@"Today‘s date is: %@", todaysDate);

if([todaysDate isEqualToDate:iPhoneReleaseDate])

NSLog(@"The iPhone was released today!");

else

NSLog(@"The iPhone was released on some other date");

NSDate *earlierDateIs = [todaysDate earlierDate:iPhoneReleaseDate];

NSLog(@"The earlier date is: %@", earlierDateIs);

NSDate *laterDateIs = [todaysDate laterDate:iPhoneReleaseDate];

NSLog(@"The later date is: %@", laterDateIs);

NSTimeInterval timeBetweenDates = [todaysDate timeIntervalSinceDate:iPhoneReleaseDate];

NSLog(@"The iPhone was released %f seconds ago", timeBetweenDates);

NSCalendar *systemCalendar = [NSCalendar currentCalendar];

unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit| NSDayCalendarUnit;

NSDateComponents *dateComparisonComponents = [systemCalendar components:unitFlags

fromDate:iPhoneReleaseDate toDate:todaysDate

options:NSWrapCalendarComponents];

NSLog(@"The iPhone was released %ld years, %ld months and %ld days ago",

dateComparisonComponents.year,

dateComparisonComponents.month,

dateComparisonComponents.day

);

你可以比较出差多少十分秒,年月日等。

4,字符串转换成日期。

NSString *dateString = @"02/14/2012";

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

df.dateFormat = @"MM/dd/yyyy";

NSDate *valentinesDay = [df dateFromString:dateString];

NSLog(@"Valentine‘s Day = %@", valentinesDay);

用dateFromString:dateString就行了。

5,

按一定格式格式化日期。

NSString *dateString = @"02/14/2012";

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

df.dateFormat = @"MM/dd/yyyy";

NSDate *valentinesDay = [df dateFromString:dateString];

NSLog(@"Unformatted Valentine‘s Day = %@", valentinesDay);

NSLog(@"Formatted Valentine‘s Day = %@", [df stringFromDate:valentinesDay]);

df.dateFormat = @"EEEE, MMMM d";

NSLog(@"Another Formatted Valentine‘s Day = %@", [df stringFromDate:valentinesDay]);

6,加减日期。

代码,

NSString *dateString = @"02/14/2012";

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

df.dateFormat = @"MM/dd/yyyy";

NSDate *valentinesDay = [df dateFromString:dateString];

NSLog(@"Valentine‘s Day = %@", valentinesDay);

NSDateComponents *weekBeforeDateComponents = [[NSDateComponents alloc] init];

weekBeforeDateComponents.week = -1;

NSDate *vDayShoppingDay = [[NSCalendar currentCalendar] dateByAddingComponents:weekBeforeDateComponents

toDate:valentinesDay

options:0];

NSLog(@"Shop for Valentine‘s Day by %@", vDayShoppingDay);

由于日期是对象,操作日期的相关属性即可,

7,使用定时器安排或重复任务。就是你想要一段代码在特定时期执行。

先弄一个日期对象

NSDate *scheduledTime = [NSDate dateWithTimeIntervalSinceNow:10.0];

创建计时器。

NSString *customUserObject = @"To demo userInfo";

NSTimer *timer = [[NSTimer alloc] initWithFireDate:scheduledTime

interval:2

target:self

selector:@selector(task)

userInfo:customUserObject

repeats:YES];

循环运行,添加时间。

NSRunLoop *runLoop = [NSRunLoop currentRunLoop];

[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];

具体代码

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{

NSDate *scheduledTime = [NSDate dateWithTimeIntervalSinceNow:10.0];

NSString *customUserObject = @"To demo userInfo";

NSTimer *timer = [[NSTimer alloc] initWithFireDate:scheduledTime

interval:2

target:self

selector:@selector(task:)

userInfo:customUserObject

repeats:YES];

NSRunLoop *runLoop = [NSRunLoop currentRunLoop];

[runLoop addTimer:timer

forMode:NSDefaultRunLoopMode];

}

-(void)task:(id)sender{

NSTimer *localTimer = (NSTimer *)sender;

NSLog(@"Schedule task has executed with this user info: %@", [localTimer userInfo]);

}

@end

自己建立,是mac应用,不是IOS应用。

时间与日期。

时间: 2024-08-28 23:09:49

时间与日期。的相关文章

MATLAB——时间,日期及显示格式

一.日期和时间 1.生成指定格式日期和时间 标准日期格式 2.获取当前时间的数值 >> datestr(now,31) ans = 2017-09-11 19:56:26 >> datestr(now,'yyyy-mm-dd HH:MM:SS') ans = 2017-09-11 19:58:28 >> clock ans = 1.0e+03 * 2.0170 0.0090 0.0110 0.0200 0.0030 0.0264 >> datestr(now

Linux基础命令的操作(时间与日期,日历,计算器)

时间与日期 如果想知道Linux系统的当前时间,使用 date 命令即可显示. 上图显示的时间是2017年01月24日,星期二,18点50分26秒.CST为时区缩写(China Standard Time)表示中国的标准时间. 如果希望只显示年月日可以通过命令 date +%Y/%m/%d 当然,如果只想知道现在几点了,可以使用命令 date +%H:%M:%S 单独将时分秒显示出来 日历 如果想看这个月的日历,可以使用 cal 命令查看. 上图显示了2017年1月份的日历,并高亮显示出了今天的

C++时间和日期

C++ 日期 & 时间 C++ 标准库没有提供所谓的日期类型.C++ 继承了 C 语言用于日期和时间操作的结构和函数.为了使用日期和时间相关的函数和结构,需要在 C++ 程序中引用 <ctime> 头文件. 有四个与时间相关的类型:clock_t.time_t.size_t 和 tm.类型 clock_t.size_t 和 time_t 能够把系统时间和日期表示为某种整数. 结构类型 tm 把日期和时间以 C 结构的形式保存,tm 结构的定义如下: struct tm { int tm

java JDK8 学习笔记——第13章 时间与日期

第十三章 时间与日期 13.1 认识时间与日期 13.1.1 时间的度量 1.格林威治标准时间GMT 格林威治标准时间的正午是太阳抵达天空最高点之时.现在已经不作为标准时间使用. 2.世界时UT世界时是借由观测远方星体跨过子午线而得,在引入UTC之前,GMT和UT是相同的. 3.国际原子时TAI 将秒的国际单位定义为铯原子辐射振动91926331770周耗费的时间,从UT的1958年开始同步. 4.世界协调时UTC 采用了闰秒修正,确保UTC与UT相差不会超过0.9秒,加入闰秒的时间通常会在6月

Problem B: 时间和日期类(III)

Problem B: 时间和日期类(III) Time Limit: 4 Sec  Memory Limit: 128 MBSubmit: 2889  Solved: 1732[Submit][Status][Web Board] Description 设计一个日期时间类,用于读取输入的数据,按格式输出日期和时间. 设计日期时间类DateTime由2个成员组成,分别是一个Date类对象和一个Time类对象: 设计DateTime类需支持以下操作: DateTime::DateTime()无参构

PHP中的时间和日期

一.date()函数 string date ( string $format [, int $timestamp ] ) 作用:将时间戳格式化为易读的时间和日期. 参数 描述 format 必需.规定时间戳的格式. timestamp 可选.规定时间戳.默认是当前时间和日期. 1.获取日期 (1).年 format格式 说明 例子 L 是否为闰年 如果是闰年为 1,否则为 0 o ISO-8601 格式年份数字.这和 Y 的值相同,只除了如果 ISO 的星期数(W)属于前一年或下一年,则用那一

localtime(取得当地目前时间和日期)

/*localtime(取得当地目前时间和日期) 相关函数 time, asctime, ctime, gmtime 表头文件 #include<time.h> 定义函数 struct tm *localtime(const time_t * timep); 函数说明 localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回.结构tm的定义请参考gmtime().此函数返回的时间日期已经转换成当地时区. 返回值 返回结

Boost库简单运用——时间与日期的处理(一)

对于时间与日期的处理一直都是一个比较复杂的问题,而C++中对于时间与日期的处理也是比较简陋的,并不像Java.C#之流提供了非常方便易用的类.但随着Boost的推出,对于时间的处理也变得越来越简单方便了,今天我们就来学习一些较为方便的处理方式: 首先,我们先来了解Boost中对时间处理的一个比较基础的类,也是后续几个类的基类--timer. timer类可以测量时间的流逝,依据平台的不同,提供了毫秒甚至微秒级别的时间控制,我们也可以用它来做一个很简单的计时器,下面,我们通过代码来解释timer的

在jqueryEasyUI界面将时间以日期加时分秒的格式显示

问题描述: oracle 10G中用户表有一个字段是日期型,数据格式为yyyy-MM-dd HH:mm:ss,前端显示时只能显示成yyyy-MM-dd 后面的 HH:mm:ss不显示. 经过一番痛苦的原因分析,发现应该将用户实体的日期类型由java.sql.Date修改成java.util.Date型,并且对 jackson进行如下处理: private void Test(MyUser user) { ObjectMapper mapper = new ObjectMapper(); Writ

Python的时间与日期

时间与日期 (time, datetime包) Python具有良好的时间和日期管理功能.实际上,计算机只会维护一个挂钟时间(wall clock time),这个时间是从某个固定时间起点到现在的时间间隔.时间起点的选择与计算机相关,但一台计算机的话,这一时间起点是固定的.其它的日期信息都是从这一时间计算得到的.此外,计算机还可以测量CPU实际上运行的时间,也就是处理器时间(processor clock time),以测量计算机性能.当CPU处于闲置状态时,处理器时间会暂停. 1.time包