iOS 时间戳操作

我们在开发中会遇到各种对于时间戳的操作,这里分享下比较常用的几种。

1.当前时间戳

1 NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[[NSDate date]timeIntervalSince1970]];
2 NSLog(@"现在时间戳%@",timeSp);

2.今天凌晨时间戳  (直接复制粘贴该方法即可,返回值即为今天凌晨时间戳



 1 #pragma mark 今天凌晨时间戳
 2
 3 -(NSString*)getLingChenTimeStamp
 4  {
 5   NSInteger year,month,day,hour,min,sec,week;
 6
 7   NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
 8
 9   NSDate *now = [NSDate date];;
10
11   NSDateComponents *comps = [[NSDateComponents alloc] init];
12
13   NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |     NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
14
15   comps = [calendar components:unitFlags fromDate:now];
16
17   year = [comps year];
18
19   week = [comps weekday];
20
21   month = [comps month];
22
23   day = [comps day];
24
25   hour = [comps hour];
26
27   min = [comps minute];
28
29   sec = [comps second];
30
31   NSString * dateString = @"";
32
33   dateString = [NSString stringWithFormat:@"%ld-%ld-%ld 00:00:00",(long)year,(long)month,(long)day];//修改此处的 00:00:00 可以得到想要的时间 例如 12:00:00
34 35   NSDateFormatter * dateFormat = [[NSDateFormatter alloc]init]; 36 37   [dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; 38   NSDate * date = [dateFormat dateFromString:dateString]; 39 40   NSString * today = [NSString stringWithFormat:@"%ld",(long)[datetimeIntervalSince1970]]; 41 42   NSLog(@"今天凌晨时间戳:%@",today); 43   return today; 44 }


3.周日凌晨时间戳 直接复制粘贴该方法即可,返回值可以是 本周周日凌晨 上周周日凌晨 下周周日凌晨时间戳

 1 -(NSString*)getSundayTime
 2 {
 3     NSIntegeryear,month,day,hour,min,sec,week;
 4
 5     NSCalendar *calendar =     [[NSCalendaralloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
 6
 7     NSDate*now = [NSDate date];;
 8
 9     NSDateComponents *comps = [[NSDateComponents alloc] init];
10
11     NSInteger unitFlags = NSCalendarUnitYear |NSCalendarUnitMonth     |NSCalendarUnitDay|NSCalendarUnitWeekday | NSCalendarUnitHour |NSCalendarUnitMinute |NSCalendarUnitSecond;
12
13     comps = [calendar components:unitFlags fromDate:now];
14
15     year = [comps year];
16
17     week = [comps weekday];
18
19     month = [comps month];
20
21     day = [comps day];
22
23     hour = [comps hour];
24
25     min = [comps minute];
26
27     sec = [comps second];
28
29     NSString * todayDateString = @"";
30
31     todayDateString = [NSString stringWithFormat:@"%ld-%ld-%ld 00:00:00",(long)year,(long)month,(long)day];//修改这里00:00:00可以得到想要的时间 例如 12:00:00
32
33
34     NSDateFormatter * todayDateFormat = [[NSDateFormatter alloc]init];
35
36     [todayDateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
37
38     NSDate * todayDate = [todayDateFormat dateFromString:todayDateString];
39
40     NSDate* thisweek = [NSDate dateWithTimeIntervalSince1970:([todayDate timeIntervalSince1970] +(8 - week) * 24*60*60)];
41
42     NSString * thisWeekStmp = [NSString stringWithFormat:@"%ld",(long)[thisweektimeIntervalSince1970]];//本周日凌晨时间戳
43
44     NSString * lastlastWeekStmp = [NSString stringWithFormat:@"%ld",(long)[thisweektimeIntervalSince1970]-3600*24*7*2];//上上周日凌晨时间戳
45
46     NSString * lastWeekStmp = [NSString stringWithFormat:@"%ld",(long)[thisweektimeIntervalSince1970]-3600*24*7];//上周日凌晨时间戳
47
48     NSString * nextWeekStmp = [NSString stringWithFormat:@"%ld",(long)[thisweektimeIntervalSince1970]+3600*24*7];//下周日凌晨时间戳
49
50     NSString * nextnextWeekStmp = [NSString stringWithFormat:@"%ld",(long)[thisweektimeIntervalSince1970]+3600*24*7*2];//下周日凌晨时间戳
51
52     NSLog(@"本周周日凌晨时间戳%@",thisWeekStmp);
53
54     NSLog(@"上周周日凌晨时间戳%@",lastWeekStmp);
55
56     NSLog(@"上上周周日凌晨时间戳%@",lastlastWeekStmp);
57
58     NSLog(@"下周日凌晨时间戳%@",nextWeekStmp);
59
60     NSLog(@"下下周日凌晨时间戳%@",nextnextWeekStmp);
61
62     return thisWeekStmp;
63 }

源码提取 :http://yunpan.cn/ccvmsdsdacw23 (提取码:b2e5)

时间: 2024-07-30 23:16:45

iOS 时间戳操作的相关文章

iOS 数据库操作(使用FMDB)

iOS 数据库操作(使用FMDB) iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.PlausibleDatabase.sqlitepersistentobjects等,FMDB (https://github.com/ccgus/fmdb) 是一款简洁.易用的封装库,这一篇文章简单介绍下FMDB的使用. 在FMDB下载文件后,工程中必须导入如下文件,并使用 libsqlite3.dylib 依赖包.

iOS——文件操作NSFileManager (创建、删除,复制,粘贴)

iOS——文件操作NSFileManager (创建.删除,复制,粘贴) iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容.iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内.默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp.Library包含Caches.Preferences目录.               上面的完整路径为:用户->资源库->Applicat

iOS数据库操作之coredata详细操作步骤

CHENYILONG Blog iOS数据库操作之coredata详细操作步骤 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong iOS应用数据存取的常用方式 ? XML属性列表 -- PList? NSKeyedArchiver 归档 ?Preference(偏好设置) ? SQLite3? Core DataCore Data简介 ? Core Data 是iOS SDK 里的一个很强大的

iOS文件操作一览

1.常见的NSFileManager文件方法 2.常见的NSFileManager目录方法 3.常用的路径工具方法 4.常用的路径工具函数 5.NSProcessInfo类方法 6.常用的NSFileHandle方法 [附]iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立.封闭.安全的空间,叫做沙盒.它一般存放着程序包文件(可执行文件).图片.音频.视频.plist文件.sqlite数据库以及其他文件. 每个应用程序都有自己的独立的存储空间(沙盒) 一般来说应用程序之

iOS关于时间、时间戳操作

#pragma mark -获取字符串形式的时间戳 -(NSString *)getTimeStampString { //获取时间和时间戳 NSDate* timeStamp = [NSDatedateWithTimeIntervalSinceNow:0]; NSTimeInterval temp=[timeStamptimeIntervalSince1970]*1000; return [NSStringstringWithFormat:@"%.0f", temp]; } #pra

iOS手势操作,拖动,轻击,捏合,旋转,长按,自定义(http://www.cnblogs.com/huangjianwu/p/4675648.html)

1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureRecognizer 的子类),开发者可以直接使用他们进行手势操作. UIPanGestureRecognizer(拖动) UIPinchGestureRecognizer(捏合) UIRotationGestureRecognizer(旋转) UITapGestureRecognizer(点按) UILo

IOS文件操作和自定义对象的归档(序列化)、反归档(反序列化)

IOS对文件操作包含文件的创建,读写,移动,删除等操作. 1.文件的创建: //设定文本框存储文件的位置 NSString *strFilePath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]; //指定存储文件的文件名 NSString *fileName=[strFilePath stringByAppendingPathComponent:@

ios多线程操作(一)—— 多线程基础与原理

一.进程 进程是指在系统中正在运行的一个应用程序,每个进程之间都是独立的,每个进程都运行在其专用且受保护的内存空间内 二.线程 一个进程要想执行任务就必须要有线程,每一个进程至少都要有一条线程. 线程是进程的基本执行单元,一个进程的所有任务都在线程中执行. 线程又分为主线程和子线程: 主线程:一个ios程序运行后,默认会开启一条线程,称为"主线程"或"UI线程",ios上任何UI的操作都在主线程上执行 子线程:除主线程外进程中得其他线程,程序中一般都将耗时的操作添加

iOS数据库操作(使用FMDB)

iOS学习笔记(十六)——数据库操作(使用FMDB) 分类: iOS开发 2013-07-15 23:19 8655人阅读 评论(5) 收藏 举报 目录(?)[+] iOS中原生的SQLite API在使用上相当不友好,在使用时,非常不便.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB.PlausibleDatabase.sqlitepersistentobjects等,FMDB (https://github.com/ccgus/fmdb) 是一款简洁.易用的封装库,这一