iOS日期相关常用方法

//根据日期返回当月的天数 (频繁调用 [NSCalendar currentCalendar] 可能存在性能问题)

- (NSUInteger)getDayCountWithDay:(NSDate *)date{
    return [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date].length;

}

//传入具体的年月日来获取星期

- (NSString *)getWeekWithYear:(NSInteger)year month:(NSInteger)month  day:(NSInteger)day{
    //根据日期获取
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:day];
    [comps setMonth:month];
    [comps setYear:year];

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *date = [gregorian dateFromComponents:comps];
    NSDateComponents *weekdayComponents = [gregorian components:NSCalendarUnitWeekday fromDate:date];
    NSInteger weekday = [weekdayComponents weekday];
    NSArray *array = [NSArray arrayWithObjects:@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六", nil];
    return array[weekday-1];
}

//距离一个时间点的某个时间长度的时间

    NSTimeInterval minute = 60*60;  //分钟
    NSTimeInterval hour = 60*60;    //小时
    NSTimeInterval day = 24*60*60;  //天
    //距离现在某个时间
    [NSDate dateWithTimeIntervalSinceNow:<#(NSTimeInterval)#>];
    //距离一个相对日期的某个时间
    [NSDate dateWithTimeInterval:<#(NSTimeInterval)#> sinceDate:<#(nonnull NSDate *)#>];
    //距离1970年1月1日的某个时间
    [NSDate dateWithTimeIntervalSince1970:<#(NSTimeInterval)#>];
    //距离2001年1月1日的某个时间
    [NSDate dateWithTimeIntervalSinceReferenceDate:<#(NSTimeInterval)#>];
时间: 2024-08-25 00:58:22

iOS日期相关常用方法的相关文章

iOS 日期相关总结

// ( NSString *) == Thu Oct 16 17:06:25 +0800 2014     // dateFormat = EEE MMM dd HH:mm:ss Z yyyy          NSDateFormatter *fmt = [[NSDateFormatteralloc] init];          // 如果是真机调试,转换这种欧美时间,需要设置locale //    fmt.locale = [[NSLocale alloc] initWithLoca

iOS与日期相关的操作

// Do any additional setup after loading the view, typically from a nib. //得到当前的日期 注意week1是星期天 NSDate *date = [NSDate date]; NSLog(@"date:%@",date); //得到(24 * 60 * 60)即24小时之前的日期,dateWithTimeIntervalSinceNow: NSDate *yesterday = [NSDate dateWithT

iOS网络相关知识总结

iOS网络相关知识总结 1.关于请求NSURLRequest? 我们经常讲的GET/POST/PUT等请求是指我们要向服务器发出的NSMutableURLRequest的类型; 我们可以设置Request的URL, HTTPMethod, HTTPHeader, HTTPBody等信息.一般发请求尽量不要使用NSURLRequest,因为它不能设置请求方式.请求超时等(总之什么都不能设置).通常发请求都使用NSMutableURLRequest,可以进行更多的设置. 补充1:因为NSURL不支持

(1)StringBuilder类和StringBuffer类 (2)日期相关的类 (3)集合框架 (4)List集合

1.StringBuilder类和StringBuffer类(查手册会用即可)1.1 基本概念 由于String类描述的字符串内容无法更改,若程序中出现大量类似的字符串时需要申请独立的内存空间单独保存,此时对内存空间的消耗比较大. 为了节省内存空间希望对字符串内容本身直接进行更改,则需要使用java.lang.StringBuilder类或者java.lang.StringBuffer类. 其中StringBuffer类是从jdk1.0开始存在,是一个线程安全的可变字符序列,效率比较低 其中St

IOS --- 日期时间格式 转换

1.如何如何将一个字符串如" 20110826134106"装化为任意的日期时间格式,下面列举两种类型: NSString* string [email protected]"20110826134106"; NSDateFormatter*inputFormatter = [[[NSDateFormatter alloc] init]autorelease]; [inputFormattersetLocale:[[[NSLocale alloc] initWith

ios UITableView 相关

tableView 实现的方法 无分组的cell #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.contacts.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRow

placeholder的字体样式改变,滚动条的颜色改变,ios日期兼容

placeholder: ::-webkit-input-placeholder { color: rgba(153, 153, 153, 0.541);font-size:12px;}:-moz-placeholder {color: rgba(153, 153, 153, 0.541);font-size:12px;}::-moz-placeholder {color: rgba(153, 153, 153, 0.541);font-size:12px;}:-ms-input-placeho

VA处理日期时间常用方法:

JAVA处理日期时间常用方法: 1.java.util.Calendar  Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR.MONTH.DAY_OF_MONTH.HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法.瞬间可用毫秒值来表示,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00.000,格里高利历)的偏移量. 例:  Java代码   1. Calendar cal = Calendar

日期相关的小函数汇总

在写日历组件和旅行相关频道开发的过程中,很多需要地方需要和日期对象打交道.拿京东旅游首页来说, 左上角的搜索框就包含了许多日期相关的交互逻辑,它包含了机票,酒店,度假等,这些都含有出发日期,到达日期等. 里面还包含了一个日期组件,在用户选择完出发日期后,会自动把到达日期定位在出发日期的下一天. 又如机票低价日历 还有度假的这种特殊日历 以下是经常需要用到日期相关小函数 一. 判断闰年这个函数在日历组件是必须要的,闰年的2月是29天,非闰年是28天 /* * 判断闰年 * @param {Numb