`
lizhuang
  • 浏览: 899841 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

NSDate 年 月 日 星期 毫秒数

ios 
阅读更多
//中国时区时间
[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"]];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
   
    NSLog(@"%@",[dateFormatter stringFromDate:date]);

//显示星期几
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];
    dateFormatter.dateFormat = @"yyyy-MM-dd EEEE";

//得到当前的日期
NSDate *date = [NSDate date];
NSLog(@"date:%@",date);

//得到(24 * 60 * 60)即24小时之前的日期,dateWithTimeIntervalSinceNow:
NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow: -(24 * 60 * 60)];
NSLog(@"yesterday:%@",yesterday);


NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];
NSDate *date = [NSDate date];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
NSInteger unitFlags = NSYearCalendarUnit |
                       NSMonthCalendarUnit |
                       NSDayCalendarUnit |
                       NSWeekdayCalendarUnit |
                       NSHourCalendarUnit |
                       NSMinuteCalendarUnit |
                       NSSecondCalendarUnit;
//int week=0;
comps = [calendar components:unitFlags fromDate:date];
int week = [comps weekday];
int year=[comps year];
int month = [comps month];
int day = [comps day];
//[formatter setDateStyle:NSDateFormatterMediumStyle];
//This sets the label with the updated time.
int hour = [comps hour];
int min = [comps minute];
int sec = [comps second];
NSLog(@"week%d",week);
NSLog(@"year%d",year);
NSLog(@"month%d",month);
NSLog(@"day%d",day);
NSLog(@"hour%d",hour);
NSLog(@"min%d",min);
NSLog(@"sec%d",sec);



//得到毫秒
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
//[dateFormatter setDateFormat:@"hh:mm:ss"]
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
NSLog(@"Date%@", [dateFormatter stringFromDate:[NSDate date]]);
[dateFormatter release];

/**
*  取最近七天的时间
**/
- (void)receiveCurrentTime:(NSNotification*)notify
{
    NSDictionary *dic = [notify userInfo];
    NSDate *currentDate = [dic objectForKey:currentTimeResult];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];
    dateFormatter.dateFormat = @"yyyy-MM-dd EEEE";
   
    NSMutableArray *dateslist = [[NSMutableArray alloc] init];
   
   
    for (int i = 0; i < 7; i++) {
        NSDate *newDate = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:([currentDate timeIntervalSinceReferenceDate] + 24*3600*i)];
        [dateslist addObject:newDate];
        NSString *str = [NSString stringWithFormat:@"%@", [dateFormatter stringFromDate:newDate]];
        [_dateList addObject:str];
    }
   
   
    NSString *defaultDateStr = [self transToSendDateString:currentDate];
    _requestSeat.dateStr = defaultDateStr;

    _dates = dateslist;
}
分享到:
评论

相关推荐

    nsdate

    在iOS和macOS开发中,`NSDate`是Objective-C中的一个核心类,用于表示时间的绝对值,即自1970年1月1日(UTC)以来的秒数。这个时间戳通常被称为Unix时间戳。`NSDate`是Foundation框架的一部分,与Swift中的`Date`类...

    iOS将时间NSDate转化为毫秒时间戳的方法示例

    iOS将时间NSDate转化为毫秒时间戳的方法示例 iOS开发中,日期和时间戳的转换是一个常见的问题。下面我们将介绍如何将NSDate类型的日期转换为毫秒时间戳,并将毫秒时间戳转换回NSDate类型。 首先,我们需要理解为...

    iOS NSdate 时间转换

    在iOS开发中,`NSDate`是苹果提供的一个核心类,用于表示特定的日期和时间。在实际应用中,我们经常需要将`NSDate`对象转换为用户友好的字符串格式,或者相反,将字符串解析成`NSDate`对象。这个过程通常涉及到`...

    Swift-NSDate-Extensions.zip

    Swift-NSDate-Extensions 是 Swift NSDate 扩展,帮助开发。 标签:Swift

    swift-NSDate总结NSDateFormatter总结

    dateFormatter.dateFormat = "yyyy年MM月dd日" let currentDate = Date() let dateString = dateFormatter.string(from: currentDate) ``` 对于`NSCalendar`,它允许你进行复杂的日期操作,比如计算两个日期之间的...

    NSDate转换农历

    它基于一个统一的时间线,即公元1970年1月1日午夜(UTC)以来的秒数。通过`NSCalendar`类,我们可以进行日期的各种操作,如添加、减去时间单位或进行日期格式化。 然而,`NSCalendar`默认支持的是格里高利历(公历...

    NSDate时间

    在iOS和macOS开发中,`NSDate`是Apple的Foundation框架中的一个核心类,用于表示绝对时间,即自1970年1月1日(UTC)以来的秒数。这个时间戳通常被称为Unix时间戳。`NSDate`对象不包含任何时区或日历信息,它仅表示一...

    iOS NSDate资料

    `:创建一个基于参考日期(2001年1月1日 GMT)并向后推移`secs`秒的新日期对象。 - `+ (id)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;`:创建一个基于1970年1月1日 GMT并向后推移`secs`秒的新日期对象。 ...

    NSDate扩展

    - `NSDate *dateAtEndOfDay`:获取当天的23:59:59日期。 通过这些扩展方法,我们可以更高效地处理`NSDate`对象,提高代码的可读性和可维护性。例如,当需要显示用户上次登录的具体日期时,我们可以直接调用`...

    NSDate分类,获得对应中国农历

    NSDateComponents则是一个容器,用来存储年、月、日、小时等日期和时间元素。 在iOS中,系统默认的日历是公历(Gregorian Calendar),但我们可以创建一个ChineseLunarCalendar实例来处理农历。苹果的Foundation...

    swift-DateHelper-NSDate在Swift中的便利扩展

    5. **时间组件提取**:获取日期中的具体时间部分,如年、月、日、小时、分钟和秒。这对于处理特定时间部分的逻辑非常方便。 6. **时间单位转换**:将时间间隔从一种单位转换为另一种,例如从秒转换为分钟或从天转换...

    iOS时间戳字符串NSDate转化demo.zip

    首先,时间戳通常以秒或毫秒为单位表示自1970年1月1日(00:00:00 GMT)以来的偏移量。在iOS中,你可以使用`+[NSTimeInterval since1970]`来获取当前时间的时间戳,它返回一个`NSTimeInterval`类型的值,这个类型实际...

    NSDate,iphone显示时间,日期的方法集合

    一旦有了`NSDate`对象,你可以通过其方法获取日期和时间的各个部分,如年、月、日等。例如: ```swift let calendar = Calendar.current let components = calendar.dateComponents([.year, .month, .day], from: ...

    iphone 开发学习基础NSDate

    object-c基础语法NSDate, 适合初学者,主要学习NSDate的设置、获取当前时间、当前时间加减秒后的时间、日期比较、日期转换成NSString等

    NSDate-RFC1123:NSDate + RFC1123 Swift实现

    `NSDate-RFC1123`是一个针对Swift的实用工具,它扩展了`NSDate`类,提供了对RFC 1123日期时间格式的支持。RFC 1123全称为“Requirements for Internet Hosts -- Application and Support”,是Internet工程任务组...

    使用 Swift 编写的 NSDate 扩展 iOS

    `AFDateHelper`还包含了对日期的拆分和重组功能,例如获取日期的年、月、日、小时、分钟和秒,以及创建新的NSDate对象。这些方法可以帮助开发者更方便地处理与日期相关的业务逻辑。 在实际项目中,`AFDateHelper`...

    NSDate相关方法

    一系列关于Date的小方法,包括NSDate和NSString的互相转换,大家自己看看每个方法的说明吧!

    NSDate-DaboExtension:NSDate分类

    - **时间间隔**:可能包含计算两个日期之间相差的秒数、分钟数、小时数、天数或周数的方法。 - **日期组件**:可能提供获取或设置日期的年份、月份、日期、小时、分钟等组成部分的方法。 - **时间戳转换**:可以方便...

    iOS 年周,年月,年月日,日期选择器,

    `NSDateComponents`用来存储日期的各个部分,如年、月、日、周等;而`UIDatePicker`则是用户交互的视图,用于展示和选择日期。 1. **创建日期选择器**: 使用`UIDatePicker`,你可以通过设置其`datePickerMode`...

Global site tag (gtag.js) - Google Analytics