代码笔记和当前时间比较

Posted 菜鸟and小白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代码笔记和当前时间比较相关的知识,希望对你有一定的参考价值。

代码:

技术分享
#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
 
    //获得指定日期的格式
    NSLog(@"---%@----",[self changeTheDate:@"2013-04-18" ByDateType:@"yyyy-M-dd"]);
    //星期几
    NSLog(@"---%@---",[self getTheWeekDay:@"2013-04-18"]);
    
    //一年的第几周,每周的第几天,比较两天是不是同一个星期
    NSDate* today = [NSDate date];
    NSLog(@"--%ld---",(long)[self didWeekOfYear:today]);
    NSLog(@"---%ld---",(long)[self didWeekDay:today]);
    NSLog(@"---%ld---",(long)[self didSameWeek:today date:today]);
    
    
}
#pragma -mark -获得指定日期的格式
/*获得指定的日期格式
 
 paramDate:日期的字符串表示:“2013-04-18”
 paramDateType:要转化的日期格式:“yyyy-M-dd”
 
 */
- (NSString *)changeTheDate:(NSString *)paramDate ByDateType:(NSString *)paramDateType{
    // 将日期转化为需要的格式 yyyy-MM-dd 转化为 yyyy-M-dd
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *tempDate = [dateFormatter dateFromString:paramDate];
    [dateFormatter setDateFormat:paramDateType];
    
    
    NSString *theDate= [dateFormatter stringFromDate:tempDate];
    NSString *result=[[NSString alloc] initWithString:theDate] ;
    return result;
    
}
#pragma -mark -星期几
/*星期几*/
- (NSString *)getTheWeekDay:(NSString *)paramDate{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-M-dd"];
    NSDate *theDate= [dateFormatter dateFromString:paramDate];
    
    NSString *result=nil;
    
    NSInteger theWeek=[self didWeekDay:theDate];
    switch (theWeek) {
        case 1:
            [email protected]"星期日";
            break;
        case 2:
            [email protected]"星期一";
            break;
        case 3:
            [email protected]"星期二";
            break;
        case 4:
            [email protected]"星期三";
            break;
        case 5:
            [email protected]"星期四";
            break;
        case 6:
            [email protected]"星期五";
            break;
        case 7:
            [email protected]"星期六";
            break;
            
        default:
            break;
    }
    return result;
    
    
    
    
}
#pragma -mark -每周的第几天
//取当前时间是星期几
- (NSInteger) didWeekDay:(NSDate*)date
{
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date];
    NSInteger tempweekday = [weekdayComponents weekday];
    return tempweekday;
}
#pragma -mark -每年的第几周
//取当前时间是每年中的第几周
- (NSInteger) didWeekOfYear:(NSDate*)date
{
    NSInteger unitFlags = NSWeekOfYearCalendarUnit;
    NSCalendar *gregorian = [NSCalendar currentCalendar];
    NSDateComponents *weekdayComponents = [gregorian components:unitFlags fromDate:date];
    NSInteger weekOfYear = [weekdayComponents weekOfYear];
    return weekOfYear;
    
}
#pragma -mark -判断是不是一个星期
//判断是不是同一个星期
- (BOOL) didSameWeek:(NSDate*)date1 date:(NSDate*)date2
{
    
    NSInteger week1 = [self didWeekOfYear:date1];
    NSInteger week2 = [self didWeekOfYear:date2];
    
    return week1 == week2;
}
@end
技术分享

 

以上是关于代码笔记和当前时间比较的主要内容,如果未能解决你的问题,请参考以下文章

Android 使用两个不同的代码片段获取当前位置 NULL

比较有用的php代码片段

常用Javascript代码片段集锦

如何获取当前显示的片段?

python学习笔记012——pdb调试

Android比较两个片段