iOS 本地时间、UTC时间、时间戳等操作、获取当前年月日
2017-03-20 21:34
417 查看
//获得当前时间并且转为字符串
-(NSString*)dateTransformToTimeString { NSDate*currentDate=[NSDatedate];//获得当前时间为UTC时间2014-07-1607:54:36UTC(UTC时间比标准时间差8小时) //转为字符串 NSDateFormatter*df=[[NSDateFormatteralloc]init];//实例化时间格式类 [dfsetDateFormat:@"yyyy-MM-ddHH:mm:ss"];//格式化 //2014-07-1607:54:36(NSString类) NSString*timeString=[dfstringFromDate:currentDate]; returntimeString; }
//获取当前时间转为时间戳
-(NSString*)dateTransformToTimeSp { UInt64recordTime=[[NSDatedate]timeIntervalSince1970]*1000;//客户端当前13位毫秒级时间戳 NSString*timeSp=[NSStringstringWithFormat:@"%llu",recordTime];//时间戳转字符串(13位毫秒级时间戳字符串) returntimeSp; }
1//时间戳字符串1469193006001(毫秒)1469193006.001(毫秒,1469193006001234(微秒)1469193006.001234(微秒)转UTC时间2016-08-11T07:00:55.611Z 2-(NSString*)timespToUTCFormat:(NSString*)timesp 3{ 4NSString*timeString=[timespstringByReplacingOccurrencesOfString:@"."withString:@""]; 5if(timeString.length>=10){ 6NSString*second=[timeStringsubstringToIndex:10]; 7NSString*milliscond=[timeStringsubstringFromIndex:10]; 8NSString*timeStampString=[NSStringstringWithFormat:@"%@.%@",second,milliscond]; 9NSTimeInterval_interval=[timeStampStringdoubleValue]; 10NSDate*date=[NSDatedateWithTimeIntervalSince1970:_interval]; 11 12NSDateFormatter*dateFormatter=[[NSDateFormatteralloc]init]; 13NSTimeZone*timeZone=[NSTimeZonetimeZoneWithName:@"UTC"]; 14[dateFormattersetTimeZone:timeZone]; 15[dateFormattersetDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"]; 16NSString*dateString=[dateFormatterstringFromDate:date]; 17 18returndateString; 19} 20return@""; 21}
//13位时间戳1469193006001(毫秒)转系统时间2016-08-1108:55:36
1+(NSString*)timespToYMDFormat:(NSNumber*)timesp 2{ 3NSString*stime=[timespstringValue]; 4NSTimeIntervaltime=[[stimesubstringToIndex:10]doubleValue]; 5NSDate*detaildate=[NSDatedateWithTimeIntervalSince1970:time]; 6NSDateFormatter*dateFormatter=[[NSDateFormatteralloc]init]; 7[dateFormattersetDateFormat:@"yyyy-MM-ddHH:mm:ss"]; 8 9return[dateFormatterstringFromDate:detaildate]; 10}
//时间转时间戳的方法:sendDate为NSDate类
NSString*timeSp=[NSStringstringWithFormat:@"%ld",(long)[sendDatetimeIntervalSince1970]];
//12-5-11添加
如果只获取当前的年月日,用NSDate直接截取是不对的,以下方法提供了获取当前的年月日等等
//获取代表公历的NSCalendar对象 NSCalendar*gregorian=[[NSCalendaralloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; //获取当前日期 //定义一个时间字段的旗标,指定将会获取指定年、月、日、时、分、秒的信息 unsignedunitFlags=NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay| NSCalendarUnitHour|NSCalendarUnitMinute| NSCalendarUnitSecond|NSCalendarUnitWeekday; //获取不同时间字段的信息 NSDateComponents*comp=[gregoriancomponents:unitFlags fromDate:localeDate]; NSIntegeryear=comp.year; //下面是可以获取的内容// @propertyNSIntegerera; @propertyNSIntegeryear; @propertyNSIntegermonth; @propertyNSIntegerday; @propertyNSIntegerhour; @propertyNSIntegerminute; @propertyNSIntegersecond; @propertyNSIntegernanosecondNS_AVAILABLE(10_7,5_0); @propertyNSIntegerweekday; @propertyNSIntegerweekdayOrdinal; @propertyNSIntegerquarterNS_AVAILABLE(10_6,4_0); @propertyNSIntegerweekOfMonthNS_AVAILABLE(10_7,5_0); @propertyNSIntegerweekOfYearNS_AVAILABLE(10_7,5_0); @propertyNSIntegeryearForWeekOfYearNS_AVAILABLE(10_7,5_0);
相关文章推荐
- iOS 本地时间 / UTC时间 / 时间戳等操作 / 获取当前年月日
- iOS 本地时间 / UTC时间 / 时间戳等操作 / 获取当前年月日
- 输入日期,计算出与当前系统日期相隔多少天(获取系统时间,年月日分别获取,对其进行格式化simpledateformat;输入的日期类似操作)
- ios获取当前时间年月日时分秒
- ios开发 获取系统时间 取得当前的年月日,当前的时分秒获得,周几和星期几获得
- NSDate相关处理,获取当前时间戳,获取本地时间,获取星期 iOS新手笔记
- IOS获取当前时区时间,并截取年月日输出字符串
- ios 时间操作 根据当前时间 获取昨天 今天 明天
- Js获取当前日期时间及其它操作
- Js获取当前日期时间及其它操作
- Js获取当前日期时间及其它操作
- 备忘录:Js获取当前日期时间及其它操作
- Js获取当前日期时间及其它操作
- Js获取当前日期时间及其它操作
- Js获取当前日期时间及其它操作
- Js获取当前日期时间及其它操作
- Js获取当前日期时间及其它操作
- Js获取当前日期时间及其它操作
- Js获取当前日期时间及其它操作
- Js获取当前日期时间及其它操作