NSDate简单介绍
NSDate简单介绍
一:NSDate是一个日期\时间方面的类,主要用来创建\获取时间
1.NSDate对象的创建:
date 创建一个当前系统日期和时间的对象
dateWithTimeIntervalSinceNow: 创建距离当前时间戳的对象
dateWithTimeInterval:sinceDate: 创建距离日期和时间对象的时间戳的对象
dateWithTimeIntervalSince1970: 创建距离1970年的时间戳对象
这里简单阐述下时间戳:时间戳的字面意思就是唯一的标识某一刻的时间,比如:2016年4月27日17点54分30秒,这就是我写这句话的时间戳
简单来说:就是从指定时间开始的那一秒数(楼主自己的理解)
2.类方法:
distantFuture 获取很久以后的日期对象
distantPast 获取很久以前的日期对象
3.日期时间比较:
isEqualToDate: 比较日期时间是否相等
earlierDate: 较早的那个日期时间
laterDate: 较晚的那个日期时间
compare: 时间先后
4.获取时间戳
timeIntervalSinceDate: 获取到指定日期时间的时间戳
timeIntervalSinceNow: 获取到现在的时间戳
timeIntervalSince1970: 获取从1970年到指定日期的时间戳
5.在日期时间对象上增加时间戳:
dateByAddingTimeInterval:返回新NSDate对象
二.代码
1.创建日期:
1 //创建一个日期时间对象 2 NSDate *nowDate = [NSDate date]; 3 // 时区 4 //2015-07-20 08:08:40 +0000 5 // + 8 6 NSLog(@"date = %@",nowDate); 7 //2)日期时间的格式化显示 8 // NSDateFormatter 类完成 9 NSDateFormatter *formatter = [NSDateFormatter new]; 10 // 指定了显示的格式 11 // formatter.dateFormat 可以赋值一个字符串,字符串就是我们要显示的格 12 formatter.dateFormat = @"yyyy年MM月dd日 HH时mm分ss秒"; 13 /* 14 15 y 表示年 16 yyyy 表示显示4位的年 2015 17 MM 表示月份(两位M表示不足两位补0) 18 dd 表示天数 19 20 HH 以24小时制显示 小时 21 hh 以12小时制小时 22 mm 显示分钟 23 ss 显示秒数 24 */ 25 //用指定的格式对我们的 nowDate进行格式化 26 // 把date 转换为字符串 27 // 按照我们指定的格式进行转换 28 NSString *dateStr = [formatter stringFromDate:nowDate]; 29 NSLog(@"dataStr = %@",dateStr); 30 31 //计算日期 32 // 当前日期 33 NSDate *date = [NSDate date]; 34 NSTimeInterval time = 60*60*24; //24小时的秒数 35 //明天的此刻 36 NSDate *mt = [date dateByAddingTimeInterval:time]; 37 NSDateFormatter *formatter0 = [NSDateFormatter new]; 38 // 指定了显示的格式 39 // formatter.dateFormat 可以赋值一个字符串,字符串就是我们要显示的格式 40 formatter0.dateFormat = @"yyyy年MM月dd日 HH时mm分ss秒"; 41 NSString *timeStr = [formatter stringFromDate:mt]; 42 NSLog(@"timeStr = %@",timeStr); 43 NSTimeInterval time1 = 60*60*24; //24小时的秒数 44 //昨天的现在 45 NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:time1]; 46 NSDateFormatter *formatter1 = [NSDateFormatter new]; 47 // 指定了显示的格式 48 // formatter.dateFormat 可以赋值一个字符串,字符串就是我们要显示的格式 49 formatter1.dateFormat = @"yyyy年MM月dd日 HH时mm分ss秒"; 50 NSString *timeStr1 = [formatter stringFromDate:date1]; 51 NSLog(@"timeStr = %@",timeStr1); 52 53 // 例: 54 //刚才返回的 2015年07月19日 16时29分16秒 字符串? 55 //假设 我要去获取日期的 年份 月份 天数 56 //NSCalendar 可以帮我们分解日期的每一部分 57 //创建日期时间对象 58 NSCalendar *cal = [NSCalendar currentCalendar]; 59 60 //获取当前日期的每一部分 要获取的部分 从哪个日期对象 61 NSDateComponents *coms = [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]]; 62 63 NSLog(@"------%ld,-----%ld,----%ld",coms.year,coms.month,coms.day);
2.打印结果:
1 2016-04-27 18:16:08.182 NADate[11660:455237] date = 2016-04-27 10:16:08 +0000 2 2016-04-27 18:16:08.183 NADate[11660:455237] dataStr = 2016年04月27日 18时16分08秒 3 2016-04-27 18:16:08.183 NADate[11660:455237] timeStr = 2016年04月28日 18时16分08秒 4 2016-04-27 18:16:08.183 NADate[11660:455237] timeStr = 2016年04月28日 18时16分08秒 5 2016-04-27 18:16:08.184 NADate[11660:455237] ------2016,-----4,----27 6 Program ended with exit code: 0
3.日期格式化:
1 // 2 // main.m 3 // NADate 4 // 5 // Created by admin on 16/4/27. 6 // Copyright © 2016年 admin. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 int main(int argc, const char * argv[]) { 12 @autoreleasepool { 13 //创建日期时间格式化对象 14 NSDateFormatter *df = [[NSDateFormatter alloc] init]; 15 16 17 // 查看时区 18 NSArray *arr = [NSTimeZone knownTimeZoneNames]; 19 NSLog(@"%@",arr); 20 21 22 df.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Chongqing"]; 23 24 //设置格式化内容 25 df.dateFormat = @"yyyy-MM-dd HH:mm:ss"; 26 27 // 把字符串转换成日期对象 28 NSDate *date = [df dateFromString:@"2005-08-30 08:00:00"]; 29 NSLog(@"%@",date); 30 // 把日期对象转换成字符串 31 NSDate *nowDate = [NSDate date]; 32 NSString *string= [df stringFromDate:nowDate]; 33 NSLog(@"%@",string); 34 35 //创建日历对象 36 // NSCalendar *calendar1 = [NSCalendar currentCalendar]; 37 // NSCalendar *calendar2 = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierChinese]; 38 // NSLog(@"%@",calendar1.calendarIdentifier); 39 // NSLog(@"%@",calendar2.calendarIdentifier); 40 41 //地区标识 42 // NSArray *arr1 = [NSLocale availableLocaleIdentifiers]; 43 // NSLog(@"%@",arr1); 44 45 // 46 NSCalendar *cl = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierChinese]; 47 //设置地区 48 cl.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"]; 49 //设置时区 50 cl.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Chongqing"]; 51 //设置每周起始 52 cl.firstWeekday = 2; 53 //获取年月日 54 NSDate *now = [NSDate date]; 55 NSInteger month,day; 56 //获取年月日的方法(NSCalendar的对象方法) 57 [cl getEra:nil year:nil month:&month day:&day fromDate:now]; 58 NSLog(@"%ld月%ld日\n",month,day);
4.打印结果:
1 2016-04-27 18:26:03.929 NADate[11684:458251] ( 2 "Africa/Abidjan", 3 "Africa/Accra", 4 "Africa/Addis_Ababa", 5 "Africa/Algiers", 6 "Africa/Asmara", 7 "Africa/Bamako", 8 "Africa/Bangui", 9 "Africa/Banjul", 10 "Africa/Bissau", 11 "Africa/Blantyre", 12 "Africa/Brazzaville", 13 "Africa/Bujumbura", 14 "Africa/Cairo", 15 "Africa/Casablanca", 16 "Africa/Ceuta", 17 "Africa/Conakry", 18 "Africa/Dakar", 19 "Africa/Dar_es_Salaam", 20 "Africa/Djibouti", 21 "Africa/Douala", 22 "Africa/El_Aaiun", 23 "Africa/Freetown", 24 "Africa/Gaborone", 25 "Africa/Harare", 26 "Africa/Johannesburg", 27 "Africa/Juba", 28 "Africa/Kampala", 29 "Africa/Khartoum", 30 "Africa/Kigali", 31 "Africa/Kinshasa", 32 "Africa/Lagos", 33 "Africa/Libreville", 34 "Africa/Lome", 35 "Africa/Luanda", 36 "Africa/Lubumbashi", 37 "Africa/Lusaka", 38 "Africa/Malabo", 39 "Africa/Maputo", 40 "Africa/Maseru", 41 "Africa/Mbabane", 42 "Africa/Mogadishu", 43 "Africa/Monrovia", 44 "Africa/Nairobi", 45 "Africa/Ndjamena", 46 "Africa/Niamey", 47 "Africa/Nouakchott", 48 "Africa/Ouagadougou", 49 "Africa/Porto-Novo", 50 "Africa/Sao_Tome", 51 "Africa/Tripoli", 52 "Africa/Tunis", 53 "Africa/Windhoek", 54 "America/Adak", 55 "America/Anchorage", 56 "America/Anguilla", 57 "America/Antigua", 58 "America/Araguaina", 59 "America/Argentina/Buenos_Aires", 60 "America/Argentina/Catamarca", 61 "America/Argentina/Cordoba", 62 "America/Argentina/Jujuy", 63 "America/Argentina/La_Rioja", 64 "America/Argentina/Mendoza", 65 "America/Argentina/Rio_Gallegos", 66 "America/Argentina/Salta", 67 "America/Argentina/San_Juan", 68 "America/Argentina/San_Luis", 69 "America/Argentina/Tucuman", 70 "America/Argentina/Ushuaia", 71 "America/Aruba", 72 "America/Asuncion", 73 "America/Atikokan", 74 "America/Bahia", 75 "America/Bahia_Banderas", 76 "America/Barbados", 77 "America/Belem", 78 "America/Belize", 79 "America/Blanc-Sablon", 80 "America/Boa_Vista", 81 "America/Bogota", 82 "America/Boise", 83 "America/Cambridge_Bay", 84 "America/Campo_Grande", 85 "America/Cancun", 86 "America/Caracas", 87 "America/Cayenne", 88 "America/Cayman", 89 "America/Chicago", 90 "America/Chihuahua", 91 "America/Costa_Rica", 92 "America/Creston", 93 "America/Cuiaba", 94 "America/Curacao", 95 "America/Danmarkshavn", 96 "America/Dawson", 97 "America/Dawson_Creek", 98 "America/Denver", 99 "America/Detroit", 100 "America/Dominica", 101 "America/Edmonton", 102 "America/Eirunepe", 103 "America/El_Salvador", 104 "America/Fort_Nelson", 105 "America/Fortaleza", 106 "America/Glace_Bay", 107 "America/Godthab", 108 "America/Goose_Bay", 109 "America/Grand_Turk", 110 "America/Grenada", 111 "America/Guadeloupe", 112 "America/Guatemala", 113 "America/Guayaquil", 114 "America/Guyana", 115 "America/Halifax", 116 "America/Havana", 117 "America/Hermosillo", 118 "America/Indiana/Indianapolis", 119 "America/Indiana/Knox", 120 "America/Indiana/Marengo", 121 "America/Indiana/Petersburg", 122 "America/Indiana/Tell_City", 123 "America/Indiana/Vevay", 124 "America/Indiana/Vincennes", 125 "America/Indiana/Winamac", 126 "America/Inuvik", 127 "America/Iqaluit", 128 "America/Jamaica", 129 "America/Juneau", 130 "America/Kentucky/Louisville", 131 "America/Kentucky/Monticello", 132 "America/Kralendijk", 133 "America/La_Paz", 134 "America/Lima", 135 "America/Los_Angeles", 136 "America/Lower_Princes", 137 "America/Maceio", 138 "America/Managua", 139 "America/Manaus", 140 "America/Marigot", 141 "America/Martinique", 142 "America/Matamoros", 143 "America/Mazatlan", 144 "America/Menominee", 145 "America/Merida", 146 "America/Metlakatla", 147 "America/Mexico_City", 148 "America/Miquelon", 149 "America/Moncton", 150 "America/Monterrey", 151 "America/Montevideo", 152 "America/Montreal", 153 "America/Montserrat", 154 "America/Nassau", 155 "America/New_York", 156 "America/Nipigon", 157 "America/Nome", 158 "America/Noronha", 159 "America/North_Dakota/Beulah", 160 "America/North_Dakota/Center", 161 "America/North_Dakota/New_Salem", 162 "America/Ojinaga", 163 "America/Panama", 164 "America/Pangnirtung", 165 "America/Paramaribo", 166 "America/Phoenix", 167 "America/Port-au-Prince", 168 "America/Port_of_Spain", 169 "America/Porto_Velho", 170 "America/Puerto_Rico", 171 "America/Rainy_River", 172 "America/Rankin_Inlet", 173 "America/Recife", 174 "America/Regina", 175 "America/Resolute", 176 "America/Rio_Branco", 177 "America/Santa_Isabel", 178 "America/Santarem", 179 "America/Santiago", 180 "America/Santo_Domingo", 181 "America/Sao_Paulo", 182 "America/Scoresbysund", 183 "America/Shiprock", 184 "America/Sitka", 185 "America/St_Barthelemy", 186 "America/St_Johns", 187 "America/St_Kitts", 188 "America/St_Lucia", 189 "America/St_Thomas", 190 "America/St_Vincent", 191 "America/Swift_Current", 192 "America/Tegucigalpa", 193 "America/Thule", 194 "America/Thunder_Bay", 195 "America/Tijuana", 196 "America/Toronto", 197 "America/Tortola", 198 "America/Vancouver", 199 "America/Whitehorse", 200 "America/Winnipeg", 201 "America/Yakutat", 202 "America/Yellowknife", 203 "Antarctica/Casey", 204 "Antarctica/Davis", 205 "Antarctica/DumontDUrville", 206 "Antarctica/Macquarie", 207 "Antarctica/Mawson", 208 "Antarctica/McMurdo", 209 "Antarctica/Palmer", 210 "Antarctica/Rothera", 211 "Antarctica/South_Pole", 212 "Antarctica/Syowa", 213 "Antarctica/Troll", 214 "Antarctica/Vostok", 215 "Arctic/Longyearbyen", 216 "Asia/Aden", 217 "Asia/Almaty", 218 "Asia/Amman", 219 "Asia/Anadyr", 220 "Asia/Aqtau", 221 "Asia/Aqtobe", 222 "Asia/Ashgabat", 223 "Asia/Baghdad", 224 "Asia/Bahrain", 225 "Asia/Baku", 226 "Asia/Bangkok", 227 "Asia/Beirut", 228 "Asia/Bishkek", 229 "Asia/Brunei", 230 "Asia/Chita", 231 "Asia/Choibalsan", 232 "Asia/Chongqing", 233 "Asia/Colombo", 234 "Asia/Damascus", 235 "Asia/Dhaka", 236 "Asia/Dili", 237 "Asia/Dubai", 238 "Asia/Dushanbe", 239 "Asia/Gaza", 240 "Asia/Harbin", 241 "Asia/Hebron", 242 "Asia/Ho_Chi_Minh", 243 "Asia/Hong_Kong", 244 "Asia/Hovd", 245 "Asia/Irkutsk", 246 "Asia/Jakarta", 247 "Asia/Jayapura", 248 "Asia/Jerusalem", 249 "Asia/Kabul", 250 "Asia/Kamchatka", 251 "Asia/Karachi", 252 "Asia/Kashgar", 253 "Asia/Kathmandu", 254 "Asia/Katmandu", 255 "Asia/Khandyga", 256 "Asia/Kolkata", 257 "Asia/Krasnoyarsk", 258 "Asia/Kuala_Lumpur", 259 "Asia/Kuching", 260 "Asia/Kuwait", 261 "Asia/Macau", 262 "Asia/Magadan", 263 "Asia/Makassar", 264 "Asia/Manila", 265 "Asia/Muscat", 266 "Asia/Nicosia", 267 "Asia/Novokuznetsk", 268 "Asia/Novosibirsk", 269 "Asia/Omsk", 270 "Asia/Oral", 271 "Asia/Phnom_Penh", 272 "Asia/Pontianak", 273 "Asia/Pyongyang", 274 "Asia/Qatar", 275 "Asia/Qyzylorda", 276 "Asia/Rangoon", 277 "Asia/Riyadh", 278 "Asia/Sakhalin", 279 "Asia/Samarkand", 280 "Asia/Seoul", 281 "Asia/Shanghai", 282 "Asia/Singapore", 283 "Asia/Srednekolymsk", 284 "Asia/Taipei", 285 "Asia/Tashkent", 286 "Asia/Tbilisi", 287 "Asia/Tehran", 288 "Asia/Thimphu", 289 "Asia/Tokyo", 290 "Asia/Ulaanbaatar", 291 "Asia/Urumqi", 292 "Asia/Ust-Nera", 293 "Asia/Vientiane", 294 "Asia/Vladivostok", 295 "Asia/Yakutsk", 296 "Asia/Yekaterinburg", 297 "Asia/Yerevan", 298 "Atlantic/Azores", 299 "Atlantic/Bermuda", 300 "Atlantic/Canary", 301 "Atlantic/Cape_Verde", 302 "Atlantic/Faroe", 303 "Atlantic/Madeira", 304 "Atlantic/Reykjavik", 305 "Atlantic/South_Georgia", 306 "Atlantic/St_Helena", 307 "Atlantic/Stanley", 308 "Australia/Adelaide", 309 "Australia/Brisbane", 310 "Australia/Broken_Hill", 311 "Australia/Currie", 312 "Australia/Darwin", 313 "Australia/Eucla", 314 "Australia/Hobart", 315 "Australia/Lindeman", 316 "Australia/Lord_Howe", 317 "Australia/Melbourne", 318 "Australia/Perth", 319 "Australia/Sydney", 320 "Europe/Amsterdam", 321 "Europe/Andorra", 322 "Europe/Athens", 323 "Europe/Belgrade", 324 "Europe/Berlin", 325 "Europe/Bratislava", 326 "Europe/Brussels", 327 "Europe/Bucharest", 328 "Europe/Budapest", 329 "Europe/Busingen", 330 "Europe/Chisinau", 331 "Europe/Copenhagen", 332 "Europe/Dublin", 333 "Europe/Gibraltar", 334 "Europe/Guernsey", 335 "Europe/Helsinki", 336 "Europe/Isle_of_Man", 337 "Europe/Istanbul", 338 "Europe/Jersey", 339 "Europe/Kaliningrad", 340 "Europe/Kiev", 341 "Europe/Lisbon", 342 "Europe/Ljubljana", 343 "Europe/London", 344 "Europe/Luxembourg", 345 "Europe/Madrid", 346 "Europe/Malta", 347 "Europe/Mariehamn", 348 "Europe/Minsk", 349 "Europe/Monaco", 350 "Europe/Moscow", 351 "Europe/Oslo", 352 "Europe/Paris", 353 "Europe/Podgorica", 354 "Europe/Prague", 355 "Europe/Riga", 356 "Europe/Rome", 357 "Europe/Samara", 358 "Europe/San_Marino", 359 "Europe/Sarajevo", 360 "Europe/Simferopol", 361 "Europe/Skopje", 362 "Europe/Sofia", 363 "Europe/Stockholm", 364 "Europe/Tallinn", 365 "Europe/Tirane", 366 "Europe/Uzhgorod", 367 "Europe/Vaduz", 368 "Europe/Vatican", 369 "Europe/Vienna", 370 "Europe/Vilnius", 371 "Europe/Volgograd", 372 "Europe/Warsaw", 373 "Europe/Zagreb", 374 "Europe/Zaporozhye", 375 "Europe/Zurich", 376 GMT, 377 "Indian/Antananarivo", 378 "Indian/Chagos", 379 "Indian/Christmas", 380 "Indian/Cocos", 381 "Indian/Comoro", 382 "Indian/Kerguelen", 383 "Indian/Mahe", 384 "Indian/Maldives", 385 "Indian/Mauritius", 386 "Indian/Mayotte", 387 "Indian/Reunion", 388 "Pacific/Apia", 389 "Pacific/Auckland", 390 "Pacific/Bougainville", 391 "Pacific/Chatham", 392 "Pacific/Chuuk", 393 "Pacific/Easter", 394 "Pacific/Efate", 395 "Pacific/Enderbury", 396 "Pacific/Fakaofo", 397 "Pacific/Fiji", 398 "Pacific/Funafuti", 399 "Pacific/Galapagos", 400 "Pacific/Gambier", 401 "Pacific/Guadalcanal", 402 "Pacific/Guam", 403 "Pacific/Honolulu", 404 "Pacific/Johnston", 405 "Pacific/Kiritimati", 406 "Pacific/Kosrae", 407 "Pacific/Kwajalein", 408 "Pacific/Majuro", 409 "Pacific/Marquesas", 410 "Pacific/Midway", 411 "Pacific/Nauru", 412 "Pacific/Niue", 413 "Pacific/Norfolk", 414 "Pacific/Noumea", 415 "Pacific/Pago_Pago", 416 "Pacific/Palau", 417 "Pacific/Pitcairn", 418 "Pacific/Pohnpei", 419 "Pacific/Ponape", 420 "Pacific/Port_Moresby", 421 "Pacific/Rarotonga", 422 "Pacific/Saipan", 423 "Pacific/Tahiti", 424 "Pacific/Tarawa", 425 "Pacific/Tongatapu", 426 "Pacific/Truk", 427 "Pacific/Wake", 428 "Pacific/Wallis" 429 ) 430 2016-04-27 18:26:04.393 NADate[11684:458251] 2005-08-30 00:00:00 +0000 431 2016-04-27 18:26:04.393 NADate[11684:458251] 2016-04-27 18:26:04 432 2016-04-27 18:26:04.394 NADate[11684:458251] 3月21日 433 Program ended with exit code: 0
打印结果中的数组,是时区代码
从430行开始是日期打印结果
三.注意
NSDate比较简单,大致分为两个步骤:1.创建日期 2.日期格式化(显示成什么格式)
还有一个需要注意的地方:
如果在打印的过程中,发现获取的时间与当前时间相差8个小时,不要担心,那是正常的,因为程序默认时间是美国时间.加上8个小时就好了.
希望能帮到需要的人,如果有什么不对的,请直接告诉我,我会用最短的时间改正.谢谢~
posted on 2016-04-27 18:23 Xib'sStory 阅读(358) 评论(0) 编辑 收藏 举报