变更记录

序号 录入时间 录入人 备注
1 2015-03-30 Alfred Jiang -
2 2015-12-23 Alfred Jiang -

方案名称

NSDate - 时间日期相关操作方法总结

关键字

NSDate \ 时间操作 \ 日期

需求场景

  1. 需要用到日期显示和时间操作等场景时

参考链接

  1. GitHub - DateTools

详细内容

######1. 时间戳相关

  1. 获取当前时间戳

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    + (NSTimeInterval)timeBySecond
    {
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    return [dat timeIntervalSince1970];
    }

    + (NSString *)stringTimeBySecond
    {
    return [NSString stringWithFormat:@"%.0f", [[self class] timeBySecond]];
    }
  2. NSDate 与 时间戳 的相互转换

    1
    2
    3
    4
    // 将 时间戳 转为 NSDate
    convenience init(timeIntervalSince1970 secs: NSTimeInterval)
    // 将 NSDate 转为 时间戳
    var timeIntervalSince1970: NSTimeInterval { get }

######2. 时间工具类推荐

  1. NSDate+DateTools.h

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    // Copyright (C) 2014 by Matthew York
    //
    // Permission is hereby granted, free of charge, to any
    // person obtaining a copy of this software and
    // associated documentation files (the "Software"), to
    // deal in the Software without restriction, including
    // without limitation the rights to use, copy, modify, merge,
    // publish, distribute, sublicense, and/or sell copies of the
    // Software, and to permit persons to whom the Software is
    // furnished to do so, subject to the following conditions:
    //
    // The above copyright notice and this permission notice shall
    // be included in all copies or substantial portions of the Software.
    //
    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
    // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
    // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

    #ifndef DateToolsLocalizedStrings
    #define DateToolsLocalizedStrings(key) \
    NSLocalizedStringFromTableInBundle(key, @"DateTools", [NSBundle bundleWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"DateTools.bundle"]], nil)
    #endif

    #import <Foundation/Foundation.h>

    static const long long SECONDS_IN_YEAR = 31556900;
    static const NSInteger SECONDS_IN_MONTH_28 = 2419200;
    static const NSInteger SECONDS_IN_MONTH_29 = 2505600;
    static const NSInteger SECONDS_IN_MONTH_30 = 2592000;
    static const NSInteger SECONDS_IN_MONTH_31 = 2678400;
    static const NSInteger SECONDS_IN_WEEK = 604800;
    static const NSInteger SECONDS_IN_DAY = 86400;
    static const NSInteger SECONDS_IN_HOUR = 3600;
    static const NSInteger SECONDS_IN_MINUTE = 60;
    static const NSInteger MILLISECONDS_IN_DAY = 86400000;

    @interface NSDate (DateTools)

    #pragma mark - Time Ago
    + (NSString*)timeAgoSinceDate:(NSDate*)date;
    + (NSString*)shortTimeAgoSinceDate:(NSDate*)date;
    - (NSString*)timeAgoSinceNow;
    - (NSString *)shortTimeAgoSinceNow;
    - (NSString *)timeAgoSinceDate:(NSDate *)date;
    - (NSString *)timeAgoSinceDate:(NSDate *)date numericDates:(BOOL)useNumericDates;
    - (NSString *)shortTimeAgoSinceDate:(NSDate *)date;

    #pragma mark - Date Components Without Calendar
    - (NSInteger)era;
    - (NSInteger)year;
    - (NSInteger)month;
    - (NSInteger)day;
    - (NSInteger)hour;
    - (NSInteger)minute;
    - (NSInteger)second;
    - (NSInteger)weekday;
    - (NSInteger)weekdayOrdinal;
    - (NSInteger)quarter;
    - (NSInteger)weekOfMonth;
    - (NSInteger)weekOfYear;
    - (NSInteger)yearForWeekOfYear;
    - (NSInteger)daysInMonth;
    - (NSInteger)dayOfYear;
    -(NSInteger)daysInYear;
    -(BOOL)isInLeapYear;
    - (BOOL)isToday;

    #pragma mark - Date Components With Calendar

    - (NSInteger)eraWithCalendar:(NSCalendar *)calendar;
    - (NSInteger)yearWithCalendar:(NSCalendar *)calendar;
    - (NSInteger)monthWithCalendar:(NSCalendar *)calendar;
    - (NSInteger)dayWithCalendar:(NSCalendar *)calendar;
    - (NSInteger)hourWithCalendar:(NSCalendar *)calendar;
    - (NSInteger)minuteWithCalendar:(NSCalendar *)calendar;
    - (NSInteger)secondWithCalendar:(NSCalendar *)calendar;
    - (NSInteger)weekdayWithCalendar:(NSCalendar *)calendar;
    - (NSInteger)weekdayOrdinalWithCalendar:(NSCalendar *)calendar;
    - (NSInteger)quarterWithCalendar:(NSCalendar *)calendar;
    - (NSInteger)weekOfMonthWithCalendar:(NSCalendar *)calendar;
    - (NSInteger)weekOfYearWithCalendar:(NSCalendar *)calendar;
    - (NSInteger)yearForWeekOfYearWithCalendar:(NSCalendar *)calendar;

    #pragma mark - Date Editing
    #pragma mark Date By Adding
    - (NSDate *)dateByAddingYears:(NSInteger)years;
    - (NSDate *)dateByAddingMonths:(NSInteger)months;
    - (NSDate *)dateByAddingWeeks:(NSInteger)weeks;
    - (NSDate *)dateByAddingDays:(NSInteger)days;
    - (NSDate *)dateByAddingHours:(NSInteger)hours;
    - (NSDate *)dateByAddingMinutes:(NSInteger)minutes;
    - (NSDate *)dateByAddingSeconds:(NSInteger)seconds;
    #pragma mark Date By Subtracting
    - (NSDate *)dateBySubtractingYears:(NSInteger)years;
    - (NSDate *)dateBySubtractingMonths:(NSInteger)months;
    - (NSDate *)dateBySubtractingWeeks:(NSInteger)weeks;
    - (NSDate *)dateBySubtractingDays:(NSInteger)days;
    - (NSDate *)dateBySubtractingHours:(NSInteger)hours;
    - (NSDate *)dateBySubtractingMinutes:(NSInteger)minutes;
    - (NSDate *)dateBySubtractingSeconds:(NSInteger)seconds;

    #pragma mark - Date Comparison
    #pragma mark Time From
    -(NSInteger)yearsFrom:(NSDate *)date;
    -(NSInteger)monthsFrom:(NSDate *)date;
    -(NSInteger)weeksFrom:(NSDate *)date;
    -(NSInteger)daysFrom:(NSDate *)date;
    -(double)hoursFrom:(NSDate *)date;
    -(double)minutesFrom:(NSDate *)date;
    -(double)secondsFrom:(NSDate *)date;
    #pragma mark Time From With Calendar
    -(NSInteger)yearsFrom:(NSDate *)date calendar:(NSCalendar *)calendar;
    -(NSInteger)monthsFrom:(NSDate *)date calendar:(NSCalendar *)calendar;
    -(NSInteger)weeksFrom:(NSDate *)date calendar:(NSCalendar *)calendar;
    -(NSInteger)daysFrom:(NSDate *)date calendar:(NSCalendar *)calendar;

    #pragma mark Time Until
    -(NSInteger)yearsUntil;
    -(NSInteger)monthsUntil;
    -(NSInteger)weeksUntil;
    -(NSInteger)daysUntil;
    -(double)hoursUntil;
    -(double)minutesUntil;
    -(double)secondsUntil;
    #pragma mark Time Ago
    -(NSInteger)yearsAgo;
    -(NSInteger)monthsAgo;
    -(NSInteger)weeksAgo;
    -(NSInteger)daysAgo;
    -(double)hoursAgo;
    -(double)minutesAgo;
    -(double)secondsAgo;
    #pragma mark Earlier Than
    -(NSInteger)yearsEarlierThan:(NSDate *)date;
    -(NSInteger)monthsEarlierThan:(NSDate *)date;
    -(NSInteger)weeksEarlierThan:(NSDate *)date;
    -(NSInteger)daysEarlierThan:(NSDate *)date;
    -(double)hoursEarlierThan:(NSDate *)date;
    -(double)minutesEarlierThan:(NSDate *)date;
    -(double)secondsEarlierThan:(NSDate *)date;
    #pragma mark Later Than
    -(NSInteger)yearsLaterThan:(NSDate *)date;
    -(NSInteger)monthsLaterThan:(NSDate *)date;
    -(NSInteger)weeksLaterThan:(NSDate *)date;
    -(NSInteger)daysLaterThan:(NSDate *)date;
    -(double)hoursLaterThan:(NSDate *)date;
    -(double)minutesLaterThan:(NSDate *)date;
    -(double)secondsLaterThan:(NSDate *)date;
    #pragma mark Comparators
    -(BOOL)isEarlierThan:(NSDate *)date;
    -(BOOL)isLaterThan:(NSDate *)date;
    -(BOOL)isEarlierThanOrEqualTo:(NSDate *)date;
    -(BOOL)isLaterThanOrEqualTo:(NSDate *)date;

    #pragma mark - Formatted Dates
    #pragma mark Formatted With Style
    -(NSString *)formattedDateWithStyle:(NSDateFormatterStyle)style;
    -(NSString *)formattedDateWithStyle:(NSDateFormatterStyle)style timeZone:(NSTimeZone *)timeZone;
    -(NSString *)formattedDateWithStyle:(NSDateFormatterStyle)style locale:(NSLocale *)locale;
    -(NSString *)formattedDateWithStyle:(NSDateFormatterStyle)style timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale;
    #pragma mark Formatted With Format
    -(NSString *)formattedDateWithFormat:(NSString *)format;
    -(NSString *)formattedDateWithFormat:(NSString *)format timeZone:(NSTimeZone *)timeZone;
    -(NSString *)formattedDateWithFormat:(NSString *)format locale:(NSLocale *)locale;
    -(NSString *)formattedDateWithFormat:(NSString *)format timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale;

    #pragma mark - Helpers
    +(NSString *)defaultCalendarIdentifier;
    + (void)setDefaultCalendarIdentifier:(NSString *)identifier;
    @end
  2. NSDate+DateTools.h

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772
    773
    774
    775
    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    791
    792
    793
    794
    795
    796
    797
    798
    799
    800
    801
    802
    803
    804
    805
    806
    807
    808
    809
    810
    811
    812
    813
    814
    815
    816
    817
    818
    819
    820
    821
    822
    823
    824
    825
    826
    827
    828
    829
    830
    831
    832
    833
    834
    835
    836
    837
    838
    839
    840
    841
    842
    843
    844
    845
    846
    847
    848
    849
    850
    851
    852
    853
    854
    855
    856
    857
    858
    859
    860
    861
    862
    863
    864
    865
    866
    867
    868
    869
    870
    871
    872
    873
    874
    875
    876
    877
    878
    879
    880
    881
    882
    883
    884
    885
    886
    887
    888
    889
    890
    891
    892
    893
    894
    895
    896
    897
    898
    899
    900
    901
    902
    903
    904
    905
    906
    907
    908
    909
    910
    911
    912
    913
    914
    915
    916
    917
    918
    919
    920
    921
    922
    923
    924
    925
    926
    927
    928
    929
    930
    931
    932
    933
    934
    935
    936
    937
    938
    939
    940
    941
    942
    943
    944
    945
    946
    947
    948
    949
    950
    951
    952
    953
    954
    955
    956
    957
    958
    959
    960
    961
    962
    963
    964
    965
    966
    967
    968
    969
    970
    971
    972
    973
    974
    975
    976
    977
    978
    979
    980
    981
    982
    983
    984
    985
    986
    987
    988
    989
    990
    991
    992
    993
    994
    995
    996
    997
    998
    999
    1000
    1001
    1002
    1003
    1004
    1005
    1006
    1007
    1008
    1009
    1010
    1011
    1012
    1013
    1014
    1015
    1016
    1017
    1018
    1019
    1020
    1021
    1022
    1023
    1024
    1025
    1026
    1027
    1028
    1029
    1030
    1031
    1032
    1033
    1034
    1035
    1036
    1037
    1038
    1039
    1040
    1041
    1042
    1043
    1044
    1045
    1046
    1047
    1048
    1049
    1050
    1051
    1052
    1053
    1054
    1055
    1056
    1057
    1058
    1059
    1060
    1061
    1062
    1063
    1064
    1065
    1066
    1067
    1068
    1069
    1070
    1071
    1072
    1073
    1074
    1075
    1076
    1077
    1078
    1079
    1080
    1081
    1082
    1083
    1084
    1085
    1086
    1087
    1088
    1089
    1090
    1091
    1092
    1093
    1094
    1095
    1096
    1097
    1098
    1099
    1100
    1101
    1102
    1103
    1104
    1105
    1106
    1107
    1108
    1109
    1110
    1111
    1112
    1113
    1114
    1115
    1116
    1117
    1118
    1119
    1120
    1121
    1122
    1123
    1124
    1125
    1126
    1127
    1128
    1129
    1130
    1131
    1132
    1133
    1134
    1135
    1136
    1137
    1138
    1139
    1140
    1141
    1142
    1143
    1144
    1145
    1146
    1147
    1148
    1149
    1150
    1151
    1152
    1153
    1154
    1155
    1156
    1157
    1158
    1159
    1160
    1161
    1162
    1163
    1164
    1165
    1166
    1167
    1168
    1169
    1170
    1171
    1172
    1173
    1174
    1175
    1176
    1177
    1178
    1179
    1180
    1181
    1182
    1183
    1184
    1185
    1186
    1187
    1188
    1189
    1190
    1191
    1192
    1193
    1194
    1195
    1196
    1197
    1198
    1199
    1200
    1201
    1202
    1203
    1204
    1205
    1206
    1207
    1208
    1209
    1210
    1211
    1212
    1213
    1214
    1215
    1216
    1217
    1218
    1219
    1220
    1221
    1222
    1223
    1224
    1225
    1226
    1227
    1228
    1229
    1230
    1231
    1232
    1233
    1234
    1235
    1236
    1237
    1238
    1239
    1240
    1241
    1242
    1243
    1244
    1245
    1246
    1247
    1248
    1249
    1250
    1251
    1252
    1253
    1254
    1255
    1256
    1257
    1258
    1259
    1260
    1261
    1262
    1263
    1264
    1265
    1266
    1267
    1268
    1269
    1270
    1271
    1272
    1273
    1274
    1275
    1276
    1277
    1278
    1279
    1280
    1281
    1282
    1283
    1284
    1285
    1286
    1287
    1288
    1289
    1290
    1291
    1292
    1293
    1294
    1295
    1296
    1297
    1298
    1299
    1300
    1301
    1302
    1303
    1304
    1305
    1306
    1307
    1308
    1309
    1310
    1311
    1312
    1313
    1314
    1315
    1316
    1317
    1318
    1319
    1320
    1321
    1322
    1323
    1324
    1325
    1326
    1327
    1328
    1329
    1330
    1331
    1332
    1333
    1334
    1335
    1336
    1337
    1338
    1339
    1340
    1341
    1342
    1343
    1344
    1345
    1346
    1347
    1348
    1349
    1350
    1351
    1352
    1353
    1354
    1355
    1356
    1357
    1358
    1359
    1360
    1361
    1362
    1363
    1364
    1365
    1366
    1367
    1368
    1369
    1370
    1371
    1372
    1373
    1374
    1375
    1376
    1377
    1378
    1379
    1380
    1381
    1382
    1383
    1384
    1385
    1386
    1387
    1388
    1389
    1390
    1391
    1392
    1393
    1394
    1395
    1396
    1397
    1398
    1399
    1400
    1401
    1402
    1403
    1404
    1405
    1406
    1407
    1408
    1409
    1410
    1411
    1412
    1413
    1414
    1415
    1416
    1417
    1418
    1419
    1420
    1421
    1422
    1423
    1424
    1425
    1426
    1427
    1428
    1429
    1430
    1431
    1432
    1433
    1434
    1435
    1436
    1437
    1438
    1439
    1440
    1441
    1442
    1443
    1444
    1445
    1446
    1447
    1448
    1449
    1450
    1451
    1452
    1453
    1454
    1455
    1456
    1457
    1458
    1459
    1460
    1461
    1462
    1463
    1464
    1465
    1466
    1467
    1468
    1469
    1470
    1471
    1472
    1473
    1474
    1475
    1476
    1477
    1478
    1479
    1480
    1481
    1482
    1483
    1484
    1485
    1486
    1487
    1488
    1489
    1490
    1491
    1492
    1493
    1494
    1495
    1496
    1497
    1498
    1499
    1500
    1501
    1502
    1503
    1504
    1505
    1506
    1507
    1508
    1509
    1510
    1511
    1512
    1513
    1514
    1515
    1516
    1517
    1518
    1519
    1520
    1521
    1522
    1523
    1524
    1525
    1526
    1527
    1528
    1529
    1530
    1531
    1532
    1533
    1534
    1535
    1536
    1537
    1538
    1539
    1540
    1541
    1542
    1543
    1544
    1545
    1546
    1547
    1548
    1549
    1550
    1551
    1552
    1553
    1554
    1555
    1556
    1557
    1558
    1559
    1560
    1561
    1562
    1563
    1564
    // Copyright (C) 2014 by Matthew York
    //
    // Permission is hereby granted, free of charge, to any
    // person obtaining a copy of this software and
    // associated documentation files (the "Software"), to
    // deal in the Software without restriction, including
    // without limitation the rights to use, copy, modify, merge,
    // publish, distribute, sublicense, and/or sell copies of the
    // Software, and to permit persons to whom the Software is
    // furnished to do so, subject to the following conditions:
    //
    // The above copyright notice and this permission notice shall
    // be included in all copies or substantial portions of the Software.
    //
    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
    // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
    // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

    #import "NSDate+DateTools.h"

    typedef NS_ENUM(NSUInteger, DTDateComponent){
    DTDateComponentEra,
    DTDateComponentYear,
    DTDateComponentMonth,
    DTDateComponentDay,
    DTDateComponentHour,
    DTDateComponentMinute,
    DTDateComponentSecond,
    DTDateComponentWeekday,
    DTDateComponentWeekdayOrdinal,
    DTDateComponentQuarter,
    DTDateComponentWeekOfMonth,
    DTDateComponentWeekOfYear,
    DTDateComponentYearForWeekOfYear,
    DTDateComponentDayOfYear
    };

    static const unsigned int allCalendarUnitFlags = NSYearCalendarUnit | NSQuarterCalendarUnit | NSMonthCalendarUnit | NSWeekOfYearCalendarUnit | NSWeekOfMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSEraCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit | NSWeekCalendarUnit;

    static NSString *defaultCalendarIdentifier = nil;
    static NSCalendar *implicitCalendar = nil;

    @implementation NSDate (DateTools)

    + (void)load {
    [self setDefaultCalendarIdentifier:NSGregorianCalendar];
    }

    #pragma mark - Time Ago

    /**
    * Takes in a date and returns a string with the most convenient unit of time representing
    * how far in the past that date is from now.
    *
    * @param NSDate - Date to be measured from now
    *
    * @return NSString - Formatted return string
    */
    + (NSString*)timeAgoSinceDate:(NSDate*)date{
    return [date timeAgoSinceDate:[NSDate date]];
    }

    /**
    * Takes in a date and returns a shortened string with the most convenient unit of time representing
    * how far in the past that date is from now.
    *
    * @param NSDate - Date to be measured from now
    *
    * @return NSString - Formatted return string
    */
    + (NSString*)shortTimeAgoSinceDate:(NSDate*)date{
    return [date shortTimeAgoSinceDate:[NSDate date]];
    }

    /**
    * Returns a string with the most convenient unit of time representing
    * how far in the past that date is from now.
    *
    * @return NSString - Formatted return string
    */
    - (NSString*)timeAgoSinceNow{
    return [self timeAgoSinceDate:[NSDate date]];
    }

    /**
    * Returns a shortened string with the most convenient unit of time representing
    * how far in the past that date is from now.
    *
    * @return NSString - Formatted return string
    */
    - (NSString *)shortTimeAgoSinceNow{
    return [self shortTimeAgoSinceDate:[NSDate date]];
    }

    - (NSString *)timeAgoSinceDate:(NSDate *)date{
    return [self timeAgoSinceDate:date numericDates:NO];
    }

    - (NSString *)timeAgoSinceDate:(NSDate *)date numericDates:(BOOL)useNumericDates{

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSMinuteCalendarUnit | NSHourCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSSecondCalendarUnit;
    NSDate *earliest = [self earlierDate:date];
    NSDate *latest = (earliest == self) ? date : self;
    NSDateComponents *components = [calendar components:unitFlags fromDate:earliest toDate:latest options:0];

    //Not Yet Implemented/Optional
    //The following strings are present in the translation files but lack logic as of 2014.04.05
    //@"Today", @"This week", @"This month", @"This year"
    //and @"This morning", @"This afternoon"

    if (components.year >= 2) {
    return [self logicLocalizedStringFromFormat:@"%%d %@years ago" withValue:components.year];
    }
    else if (components.year >= 1) {

    if (useNumericDates) {
    return DateToolsLocalizedStrings(@"1 year ago");
    }

    return DateToolsLocalizedStrings(@"Last year");
    }
    else if (components.month >= 2) {
    return [self logicLocalizedStringFromFormat:@"%%d %@months ago" withValue:components.month];
    }
    else if (components.month >= 1) {

    if (useNumericDates) {
    return DateToolsLocalizedStrings(@"1 month ago");
    }

    return DateToolsLocalizedStrings(@"Last month");
    }
    else if (components.weekOfMonth >= 2) {
    return [self logicLocalizedStringFromFormat:@"%%d %@weeks ago" withValue:components.weekOfMonth];
    }
    else if (components.weekOfMonth >= 1) {

    if (useNumericDates) {
    return DateToolsLocalizedStrings(@"1 week ago");
    }

    return DateToolsLocalizedStrings(@"Last week");
    }
    else if (components.day >= 2) {
    return [self logicLocalizedStringFromFormat:@"%%d %@days ago" withValue:components.day];
    }
    else if (components.day >= 1) {

    if (useNumericDates) {
    return DateToolsLocalizedStrings(@"1 day ago");
    }

    return DateToolsLocalizedStrings(@"Yesterday");
    }
    else if (components.hour >= 2) {
    return [self logicLocalizedStringFromFormat:@"%%d %@hours ago" withValue:components.hour];
    }
    else if (components.hour >= 1) {
    return DateToolsLocalizedStrings(@"An hour ago");
    }
    else if (components.minute >= 2) {
    return [self logicLocalizedStringFromFormat:@"%%d %@minutes ago" withValue:components.minute];
    }
    else if (components.minute >= 1) {
    return DateToolsLocalizedStrings(@"A minute ago");
    }
    else if (components.second >= 3) {
    return [self logicLocalizedStringFromFormat:@"%%d %@seconds ago" withValue:components.second];
    }
    else {
    return DateToolsLocalizedStrings(@"Just now");
    }

    }

    - (NSString *)shortTimeAgoSinceDate:(NSDate *)date{

    //If shortened formatting is requested, drop the "ago" part of the time ago
    //use abbreviated unit names

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSMinuteCalendarUnit | NSHourCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit | NSSecondCalendarUnit;
    NSDate *earliest = [self earlierDate:date];
    NSDate *latest = (earliest == self) ? date : self;
    NSDateComponents *components = [calendar components:unitFlags fromDate:earliest toDate:latest options:0];

    if (components.year >= 1) {
    return [self logicLocalizedStringFromFormat:@"%%d%@y" withValue:components.year];
    }
    else if (components.month >= 1) {
    return [self logicLocalizedStringFromFormat:@"%%d%@M" withValue:components.month];
    }
    else if (components.weekOfMonth >= 1) {
    return [self logicLocalizedStringFromFormat:@"%%d%@w" withValue:components.weekOfMonth];
    }
    else if (components.day >= 1) {
    return [self logicLocalizedStringFromFormat:@"%%d%@d" withValue:components.day];
    }
    else if (components.hour >= 1) {
    return [self logicLocalizedStringFromFormat:@"%%d%@h" withValue:components.hour];
    }
    else if (components.minute >= 1) {
    return [self logicLocalizedStringFromFormat:@"%%d%@m" withValue:components.minute];
    }
    else if (components.second >= 3) {
    return [self logicLocalizedStringFromFormat:@"%%d%@s" withValue:components.second];
    }
    else {
    return [self logicLocalizedStringFromFormat:@"%%d%@s" withValue:components.second];
    //return DateToolsLocalizedStrings(@"Now"); //string not yet translated 2014.04.05
    }

    }

    - (NSString *) logicLocalizedStringFromFormat:(NSString *)format withValue:(NSInteger)value{
    NSString * localeFormat = [NSString stringWithFormat:format, [self getLocaleFormatUnderscoresWithValue:value]];
    return [NSString stringWithFormat:DateToolsLocalizedStrings(localeFormat), value];
    }

    - (NSString *)getLocaleFormatUnderscoresWithValue:(double)value{
    NSString *localeCode = [[NSLocale preferredLanguages] objectAtIndex:0];

    // Russian (ru)
    if([localeCode isEqual:@"ru"]) {
    int XY = (int)floor(value) % 100;
    int Y = (int)floor(value) % 10;

    if(Y == 0 || Y > 4 || (XY > 10 && XY < 15)) {
    return @"";
    }

    if(Y > 1 && Y < 5 && (XY < 10 || XY > 20)) {
    return @"_";
    }

    if(Y == 1 && XY != 11) {
    return @"__";
    }
    }

    // Add more languages here, which are have specific translation rules...

    return @"";
    }

    #pragma mark - Date Components Without Calendar
    /**
    * Returns the era of the receiver. (0 for BC, 1 for AD for Gregorian)
    *
    * @return NSInteger
    */
    - (NSInteger)era{
    return [self componentForDate:self type:DTDateComponentEra calendar:nil];
    }

    /**
    * Returns the year of the receiver.
    *
    * @return NSInteger
    */
    - (NSInteger)year{
    return [self componentForDate:self type:DTDateComponentYear calendar:nil];
    }

    /**
    * Returns the month of the year of the receiver.
    *
    * @return NSInteger
    */
    - (NSInteger)month{
    return [self componentForDate:self type:DTDateComponentMonth calendar:nil];
    }

    /**
    * Returns the day of the month of the receiver.
    *
    * @return NSInteger
    */
    - (NSInteger)day{
    return [self componentForDate:self type:DTDateComponentDay calendar:nil];
    }

    /**
    * Returns the hour of the day of the receiver. (0-24)
    *
    * @return NSInteger
    */
    - (NSInteger)hour{
    return [self componentForDate:self type:DTDateComponentHour calendar:nil];
    }

    /**
    * Returns the minute of the receiver. (0-59)
    *
    * @return NSInteger
    */
    - (NSInteger)minute{
    return [self componentForDate:self type:DTDateComponentMinute calendar:nil];
    }

    /**
    * Returns the second of the receiver. (0-59)
    *
    * @return NSInteger
    */
    - (NSInteger)second{
    return [self componentForDate:self type:DTDateComponentSecond calendar:nil];
    }

    /**
    * Returns the day of the week of the receiver.
    *
    * @return NSInteger
    */
    - (NSInteger)weekday{
    return [self componentForDate:self type:DTDateComponentWeekday calendar:nil];
    }

    /**
    * Returns the ordinal for the day of the week of the receiver.
    *
    * @return NSInteger
    */
    - (NSInteger)weekdayOrdinal{
    return [self componentForDate:self type:DTDateComponentWeekdayOrdinal calendar:nil];
    }

    /**
    * Returns the quarter of the receiver.
    *
    * @return NSInteger
    */
    - (NSInteger)quarter{
    return [self componentForDate:self type:DTDateComponentQuarter calendar:nil];
    }

    /**
    * Returns the week of the month of the receiver.
    *
    * @return NSInteger
    */
    - (NSInteger)weekOfMonth{
    return [self componentForDate:self type:DTDateComponentWeekOfMonth calendar:nil];
    }

    /**
    * Returns the week of the year of the receiver.
    *
    * @return NSInteger
    */
    - (NSInteger)weekOfYear{
    return [self componentForDate:self type:DTDateComponentWeekOfYear calendar:nil];
    }

    /**
    * I honestly don't know much about this value...
    *
    * @return NSInteger
    */
    - (NSInteger)yearForWeekOfYear{
    return [self componentForDate:self type:DTDateComponentYearForWeekOfYear calendar:nil];
    }

    /**
    * Returns how many days are in the month of the receiver.
    *
    * @return NSInteger
    */
    - (NSInteger)daysInMonth{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSRange days = [calendar rangeOfUnit:NSDayCalendarUnit
    inUnit:NSMonthCalendarUnit
    forDate:self];
    return days.length;
    }

    /**
    * Returns the day of the year of the receiver. (1-365 or 1-366 for leap year)
    *
    * @return NSInteger
    */
    - (NSInteger)dayOfYear{
    return [self componentForDate:self type:DTDateComponentDayOfYear calendar:nil];
    }

    /**
    * Returns how many days are in the year of the receiver.
    *
    * @return NSInteger
    */
    -(NSInteger)daysInYear{
    if (self.isInLeapYear) {
    return 366;
    }

    return 365;
    }

    /**
    * Returns whether the receiver falls in a leap year.
    *
    * @return NSInteger
    */
    -(BOOL)isInLeapYear{
    NSCalendar *calendar = [[self class] implicitCalendar];
    NSDateComponents *dateComponents = [calendar components:allCalendarUnitFlags fromDate:self];

    if (dateComponents.year%400 == 0){
    return YES;
    }
    else if (dateComponents.year%100 == 0){
    return NO;
    }
    else if (dateComponents.year%4 == 0){
    return YES;
    }

    return NO;
    }

    - (BOOL)isToday {
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:[NSDate date]];
    NSDate *today = [cal dateFromComponents:components];
    components = [cal components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:self];
    NSDate *otherDate = [cal dateFromComponents:components];

    return [today isEqualToDate:otherDate];
    }

    #pragma mark - Date Components With Calendar
    /**
    * Returns the era of the receiver from a given calendar
    *
    * @param calendar NSCalendar - The calendar to be used in the calculation
    *
    * @return NSInteger - represents the era (0 for BC, 1 for AD for Gregorian)
    */
    - (NSInteger)eraWithCalendar:(NSCalendar *)calendar{
    return [self componentForDate:self type:DTDateComponentEra calendar:calendar];
    }

    /**
    * Returns the year of the receiver from a given calendar
    *
    * @param calendar NSCalendar - The calendar to be used in the calculation
    *
    * @return NSInteger - represents the year as an integer
    */
    - (NSInteger)yearWithCalendar:(NSCalendar *)calendar{
    return [self componentForDate:self type:DTDateComponentYear calendar:calendar];
    }

    /**
    * Returns the month of the receiver from a given calendar
    *
    * @param calendar NSCalendar - The calendar to be used in the calculation
    *
    * @return NSInteger - represents the month as an integer
    */
    - (NSInteger)monthWithCalendar:(NSCalendar *)calendar{
    return [self componentForDate:self type:DTDateComponentMonth calendar:calendar];
    }

    /**
    * Returns the day of the month of the receiver from a given calendar
    *
    * @param calendar NSCalendar - The calendar to be used in the calculation
    *
    * @return NSInteger - represents the day of the month as an integer
    */
    - (NSInteger)dayWithCalendar:(NSCalendar *)calendar{
    return [self componentForDate:self type:DTDateComponentDay calendar:calendar];
    }

    /**
    * Returns the hour of the day of the receiver from a given calendar
    *
    * @param calendar NSCalendar - The calendar to be used in the calculation
    *
    * @return NSInteger - represents the hour of the day as an integer
    */
    - (NSInteger)hourWithCalendar:(NSCalendar *)calendar{
    return [self componentForDate:self type:DTDateComponentHour calendar:calendar];
    }

    /**
    * Returns the minute of the hour of the receiver from a given calendar
    *
    * @param calendar NSCalendar - The calendar to be used in the calculation
    *
    * @return NSInteger - represents the minute of the hour as an integer
    */
    - (NSInteger)minuteWithCalendar:(NSCalendar *)calendar{
    return [self componentForDate:self type:DTDateComponentMinute calendar:calendar];
    }

    /**
    * Returns the second of the receiver from a given calendar
    *
    * @param calendar NSCalendar - The calendar to be used in the calculation
    *
    * @return NSInteger - represents the second as an integer
    */
    - (NSInteger)secondWithCalendar:(NSCalendar *)calendar{
    return [self componentForDate:self type:DTDateComponentSecond calendar:calendar];
    }

    /**
    * Returns the weekday of the receiver from a given calendar
    *
    * @param calendar NSCalendar - The calendar to be used in the calculation
    *
    * @return NSInteger - represents the weekday as an integer
    */
    - (NSInteger)weekdayWithCalendar:(NSCalendar *)calendar{
    return [self componentForDate:self type:DTDateComponentWeekday calendar:calendar];
    }

    /**
    * Returns the weekday ordinal of the receiver from a given calendar
    *
    * @param calendar NSCalendar - The calendar to be used in the calculation
    *
    * @return NSInteger - represents the weekday ordinal as an integer
    */
    - (NSInteger)weekdayOrdinalWithCalendar:(NSCalendar *)calendar{
    return [self componentForDate:self type:DTDateComponentWeekdayOrdinal calendar:calendar];
    }

    /**
    * Returns the quarter of the receiver from a given calendar
    *
    * @param calendar NSCalendar - The calendar to be used in the calculation
    *
    * @return NSInteger - represents the quarter as an integer
    */
    - (NSInteger)quarterWithCalendar:(NSCalendar *)calendar{
    return [self componentForDate:self type:DTDateComponentQuarter calendar:calendar];
    }

    /**
    * Returns the week of the month of the receiver from a given calendar
    *
    * @param calendar NSCalendar - The calendar to be used in the calculation
    *
    * @return NSInteger - represents the week of the month as an integer
    */
    - (NSInteger)weekOfMonthWithCalendar:(NSCalendar *)calendar{
    return [self componentForDate:self type:DTDateComponentWeekOfMonth calendar:calendar];
    }

    /**
    * Returns the week of the year of the receiver from a given calendar
    *
    * @param calendar NSCalendar - The calendar to be used in the calculation
    *
    * @return NSInteger - represents the week of the year as an integer
    */
    - (NSInteger)weekOfYearWithCalendar:(NSCalendar *)calendar{
    return [self componentForDate:self type:DTDateComponentWeekOfYear calendar:calendar];
    }

    /**
    * Returns the year for week of the year (???) of the receiver from a given calendar
    *
    * @param calendar NSCalendar - The calendar to be used in the calculation
    *
    * @return NSInteger - represents the year for week of the year as an integer
    */
    - (NSInteger)yearForWeekOfYearWithCalendar:(NSCalendar *)calendar{
    return [self componentForDate:self type:DTDateComponentYearForWeekOfYear calendar:calendar];
    }

    /**
    * Returns the day of the year of the receiver from a given calendar
    *
    * @param calendar NSCalendar - The calendar to be used in the calculation
    *
    * @return NSInteger - represents the day of the year as an integer
    */
    - (NSInteger)dayOfYearWithCalendar:(NSCalendar *)calendar{
    return [self componentForDate:self type:DTDateComponentDayOfYear calendar:calendar];
    }

    /**
    * Takes in a date, calendar and desired date component and returns the desired NSInteger
    * representation for that component
    *
    * @param date NSDate - The date to be be mined for a desired component
    * @param component DTDateComponent - The desired component (i.e. year, day, week, etc)
    * @param calendar NSCalendar - The calendar to be used in the processing (Defaults to Gregorian)
    *
    * @return NSInteger
    */
    -(NSInteger)componentForDate:(NSDate *)date type:(DTDateComponent)component calendar:(NSCalendar *)calendar{
    if (!calendar) {
    calendar = [[self class] implicitCalendar];
    }

    unsigned int unitFlags = 0;

    if (component == DTDateComponentYearForWeekOfYear) {
    unitFlags = NSYearCalendarUnit | NSQuarterCalendarUnit | NSMonthCalendarUnit | NSWeekOfYearCalendarUnit | NSWeekOfMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSEraCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit | NSWeekCalendarUnit | NSYearForWeekOfYearCalendarUnit;
    }
    else {
    unitFlags = allCalendarUnitFlags;
    }

    NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];

    switch (component) {
    case DTDateComponentEra:
    return [dateComponents era];
    case DTDateComponentYear:
    return [dateComponents year];
    case DTDateComponentMonth:
    return [dateComponents month];
    case DTDateComponentDay:
    return [dateComponents day];
    case DTDateComponentHour:
    return [dateComponents hour];
    case DTDateComponentMinute:
    return [dateComponents minute];
    case DTDateComponentSecond:
    return [dateComponents second];
    case DTDateComponentWeekday:
    return [dateComponents weekday];
    case DTDateComponentWeekdayOrdinal:
    return [dateComponents weekdayOrdinal];
    case DTDateComponentQuarter:
    return [dateComponents quarter];
    case DTDateComponentWeekOfMonth:
    return [dateComponents weekOfMonth];
    case DTDateComponentWeekOfYear:
    return [dateComponents weekOfYear];
    case DTDateComponentYearForWeekOfYear:
    return [dateComponents yearForWeekOfYear];
    case DTDateComponentDayOfYear:
    return [calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:date];
    default:
    break;
    }

    return 0;
    }

    #pragma mark - Date Editing
    #pragma mark Date By Adding
    /**
    * Returns a date representing the receivers date shifted later by the provided number of years.
    *
    * @param years NSInteger - Number of years to add
    *
    * @return NSDate - Date modified by the number of desired years
    */
    - (NSDate *)dateByAddingYears:(NSInteger)years{
    NSCalendar *calendar = [[self class] implicitCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setYear:years];

    return [calendar dateByAddingComponents:components toDate:self options:0];
    }

    /**
    * Returns a date representing the receivers date shifted later by the provided number of months.
    *
    * @param years NSInteger - Number of months to add
    *
    * @return NSDate - Date modified by the number of desired months
    */
    - (NSDate *)dateByAddingMonths:(NSInteger)months{
    NSCalendar *calendar = [[self class] implicitCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setMonth:months];

    return [calendar dateByAddingComponents:components toDate:self options:0];
    }

    /**
    * Returns a date representing the receivers date shifted later by the provided number of weeks.
    *
    * @param years NSInteger - Number of weeks to add
    *
    * @return NSDate - Date modified by the number of desired weeks
    */
    - (NSDate *)dateByAddingWeeks:(NSInteger)weeks{
    NSCalendar *calendar = [[self class] implicitCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setWeekOfMonth:weeks];

    return [calendar dateByAddingComponents:components toDate:self options:0];
    }

    /**
    * Returns a date representing the receivers date shifted later by the provided number of days.
    *
    * @param years NSInteger - Number of days to add
    *
    * @return NSDate - Date modified by the number of desired days
    */
    - (NSDate *)dateByAddingDays:(NSInteger)days{
    NSCalendar *calendar = [[self class] implicitCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:days];

    return [calendar dateByAddingComponents:components toDate:self options:0];
    }

    /**
    * Returns a date representing the receivers date shifted later by the provided number of hours.
    *
    * @param years NSInteger - Number of hours to add
    *
    * @return NSDate - Date modified by the number of desired hours
    */
    - (NSDate *)dateByAddingHours:(NSInteger)hours{
    NSCalendar *calendar = [[self class] implicitCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setHour:hours];

    return [calendar dateByAddingComponents:components toDate:self options:0];
    }

    /**
    * Returns a date representing the receivers date shifted later by the provided number of minutes.
    *
    * @param years NSInteger - Number of minutes to add
    *
    * @return NSDate - Date modified by the number of desired minutes
    */
    - (NSDate *)dateByAddingMinutes:(NSInteger)minutes{
    NSCalendar *calendar = [[self class] implicitCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setMinute:minutes];

    return [calendar dateByAddingComponents:components toDate:self options:0];
    }

    /**
    * Returns a date representing the receivers date shifted later by the provided number of seconds.
    *
    * @param years NSInteger - Number of seconds to add
    *
    * @return NSDate - Date modified by the number of desired seconds
    */
    - (NSDate *)dateByAddingSeconds:(NSInteger)seconds{
    NSCalendar *calendar = [[self class] implicitCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setSecond:seconds];

    return [calendar dateByAddingComponents:components toDate:self options:0];
    }

    #pragma mark Date By Subtracting
    /**
    * Returns a date representing the receivers date shifted earlier by the provided number of years.
    *
    * @param years NSInteger - Number of years to subtract
    *
    * @return NSDate - Date modified by the number of desired years
    */
    - (NSDate *)dateBySubtractingYears:(NSInteger)years{
    NSCalendar *calendar = [[self class] implicitCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setYear:-1*years];

    return [calendar dateByAddingComponents:components toDate:self options:0];
    }

    /**
    * Returns a date representing the receivers date shifted earlier by the provided number of months.
    *
    * @param years NSInteger - Number of months to subtract
    *
    * @return NSDate - Date modified by the number of desired months
    */
    - (NSDate *)dateBySubtractingMonths:(NSInteger)months{
    NSCalendar *calendar = [[self class] implicitCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setMonth:-1*months];

    return [calendar dateByAddingComponents:components toDate:self options:0];
    }

    /**
    * Returns a date representing the receivers date shifted earlier by the provided number of weeks.
    *
    * @param years NSInteger - Number of weeks to subtract
    *
    * @return NSDate - Date modified by the number of desired weeks
    */
    - (NSDate *)dateBySubtractingWeeks:(NSInteger)weeks{
    NSCalendar *calendar = [[self class] implicitCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setWeekOfMonth:-1*weeks];

    return [calendar dateByAddingComponents:components toDate:self options:0];
    }

    /**
    * Returns a date representing the receivers date shifted earlier by the provided number of days.
    *
    * @param years NSInteger - Number of days to subtract
    *
    * @return NSDate - Date modified by the number of desired days
    */
    - (NSDate *)dateBySubtractingDays:(NSInteger)days{
    NSCalendar *calendar = [[self class] implicitCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:-1*days];

    return [calendar dateByAddingComponents:components toDate:self options:0];
    }

    /**
    * Returns a date representing the receivers date shifted earlier by the provided number of hours.
    *
    * @param years NSInteger - Number of hours to subtract
    *
    * @return NSDate - Date modified by the number of desired hours
    */
    - (NSDate *)dateBySubtractingHours:(NSInteger)hours{
    NSCalendar *calendar = [[self class] implicitCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setHour:-1*hours];

    return [calendar dateByAddingComponents:components toDate:self options:0];
    }

    /**
    * Returns a date representing the receivers date shifted earlier by the provided number of minutes.
    *
    * @param years NSInteger - Number of minutes to subtract
    *
    * @return NSDate - Date modified by the number of desired minutes
    */
    - (NSDate *)dateBySubtractingMinutes:(NSInteger)minutes{
    NSCalendar *calendar = [[self class] implicitCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setMinute:-1*minutes];

    return [calendar dateByAddingComponents:components toDate:self options:0];
    }

    /**
    * Returns a date representing the receivers date shifted earlier by the provided number of seconds.
    *
    * @param years NSInteger - Number of seconds to subtract
    *
    * @return NSDate - Date modified by the number of desired seconds
    */
    - (NSDate *)dateBySubtractingSeconds:(NSInteger)seconds{
    NSCalendar *calendar = [[self class] implicitCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setSecond:-1*seconds];

    return [calendar dateByAddingComponents:components toDate:self options:0];
    }

    #pragma mark - Date Comparison
    #pragma mark Time From
    /**
    * Returns an NSInteger representing the amount of time in years between the receiver and the provided date.
    * If the receiver is earlier than the provided date, the returned value will be negative.
    * Uses the default Gregorian calendar
    *
    * @param date NSDate - The provided date for comparison
    *
    * @return NSInteger - The NSInteger representation of the years between receiver and provided date
    */
    -(NSInteger)yearsFrom:(NSDate *)date{
    return [self yearsFrom:date calendar:nil];
    }

    /**
    * Returns an NSInteger representing the amount of time in months between the receiver and the provided date.
    * If the receiver is earlier than the provided date, the returned value will be negative.
    * Uses the default Gregorian calendar
    *
    * @param date NSDate - The provided date for comparison
    *
    * @return NSInteger - The NSInteger representation of the years between receiver and provided date
    */
    -(NSInteger)monthsFrom:(NSDate *)date{
    return [self monthsFrom:date calendar:nil];
    }

    /**
    * Returns an NSInteger representing the amount of time in weeks between the receiver and the provided date.
    * If the receiver is earlier than the provided date, the returned value will be negative.
    * Uses the default Gregorian calendar
    *
    * @param date NSDate - The provided date for comparison
    *
    * @return NSInteger - The double representation of the weeks between receiver and provided date
    */
    -(NSInteger)weeksFrom:(NSDate *)date{
    return [self weeksFrom:date calendar:nil];
    }

    /**
    * Returns an NSInteger representing the amount of time in days between the receiver and the provided date.
    * If the receiver is earlier than the provided date, the returned value will be negative.
    * Uses the default Gregorian calendar
    *
    * @param date NSDate - The provided date for comparison
    *
    * @return NSInteger - The double representation of the days between receiver and provided date
    */
    -(NSInteger)daysFrom:(NSDate *)date{
    return [self daysFrom:date calendar:nil];
    }

    /**
    * Returns an NSInteger representing the amount of time in hours between the receiver and the provided date.
    * If the receiver is earlier than the provided date, the returned value will be negative.
    *
    * @param date NSDate - The provided date for comparison
    *
    * @return double - The double representation of the hours between receiver and provided date
    */
    -(double)hoursFrom:(NSDate *)date{
    return ([self timeIntervalSinceDate:date])/SECONDS_IN_HOUR;
    }

    /**
    * Returns an NSInteger representing the amount of time in minutes between the receiver and the provided date.
    * If the receiver is earlier than the provided date, the returned value will be negative.
    *
    * @param date NSDate - The provided date for comparison
    *
    * @return double - The double representation of the minutes between receiver and provided date
    */
    -(double)minutesFrom:(NSDate *)date{
    return ([self timeIntervalSinceDate:date])/SECONDS_IN_MINUTE;
    }

    /**
    * Returns an NSInteger representing the amount of time in seconds between the receiver and the provided date.
    * If the receiver is earlier than the provided date, the returned value will be negative.
    *
    * @param date NSDate - The provided date for comparison
    *
    * @return double - The double representation of the seconds between receiver and provided date
    */
    -(double)secondsFrom:(NSDate *)date{
    return [self timeIntervalSinceDate:date];
    }

    #pragma mark Time From With Calendar
    /**
    * Returns an NSInteger representing the amount of time in years between the receiver and the provided date.
    * If the receiver is earlier than the provided date, the returned value will be negative.
    *
    * @param date NSDate - The provided date for comparison
    * @param calendar NSCalendar - The calendar to be used in the calculation
    *
    * @return NSInteger - The double representation of the years between receiver and provided date
    */
    -(NSInteger)yearsFrom:(NSDate *)date calendar:(NSCalendar *)calendar{
    if (!calendar) {
    calendar = [[self class] implicitCalendar];
    }

    NSDate *earliest = [self earlierDate:date];
    NSDate *latest = (earliest == self) ? date : self;
    NSInteger multiplier = (earliest == self) ? -1 : 1;
    NSDateComponents *components = [calendar components:NSYearCalendarUnit fromDate:earliest toDate:latest options:0];
    return multiplier*components.year;
    }

    /**
    * Returns an NSInteger representing the amount of time in months between the receiver and the provided date.
    * If the receiver is earlier than the provided date, the returned value will be negative.
    *
    * @param date NSDate - The provided date for comparison
    * @param calendar NSCalendar - The calendar to be used in the calculation
    *
    * @return NSInteger - The double representation of the months between receiver and provided date
    */
    -(NSInteger)monthsFrom:(NSDate *)date calendar:(NSCalendar *)calendar{
    if (!calendar) {
    calendar = [[self class] implicitCalendar];
    }

    NSDate *earliest = [self earlierDate:date];
    NSDate *latest = (earliest == self) ? date : self;
    NSInteger multiplier = (earliest == self) ? -1 : 1;
    NSDateComponents *components = [calendar components:allCalendarUnitFlags fromDate:earliest toDate:latest options:0];
    return multiplier*(components.month + 12*components.year);
    }

    /**
    * Returns an NSInteger representing the amount of time in weeks between the receiver and the provided date.
    * If the receiver is earlier than the provided date, the returned value will be negative.
    *
    * @param date NSDate - The provided date for comparison
    * @param calendar NSCalendar - The calendar to be used in the calculation
    *
    * @return NSInteger - The double representation of the weeks between receiver and provided date
    */
    -(NSInteger)weeksFrom:(NSDate *)date calendar:(NSCalendar *)calendar{
    if (!calendar) {
    calendar = [[self class] implicitCalendar];
    }

    NSDate *earliest = [self earlierDate:date];
    NSDate *latest = (earliest == self) ? date : self;
    NSInteger multiplier = (earliest == self) ? -1 : 1;
    NSDateComponents *components = [calendar components:NSWeekCalendarUnit fromDate:earliest toDate:latest options:0];
    return multiplier*components.weekOfMonth;
    }

    /**
    * Returns an NSInteger representing the amount of time in days between the receiver and the provided date.
    * If the receiver is earlier than the provided date, the returned value will be negative.
    *
    * @param date NSDate - The provided date for comparison
    * @param calendar NSCalendar - The calendar to be used in the calculation
    *
    * @return NSInteger - The double representation of the days between receiver and provided date
    */
    -(NSInteger)daysFrom:(NSDate *)date calendar:(NSCalendar *)calendar{
    if (!calendar) {
    calendar = [[self class] implicitCalendar];
    }

    NSDate *earliest = [self earlierDate:date];
    NSDate *latest = (earliest == self) ? date : self;
    NSInteger multiplier = (earliest == self) ? -1 : 1;
    NSDateComponents *components = [calendar components:NSDayCalendarUnit fromDate:earliest toDate:latest options:0];
    return multiplier*components.day;
    }

    #pragma mark Time Until
    /**
    * Returns the number of years until the receiver's date. Returns 0 if the receiver is the same or earlier than now.
    *
    * @return NSInteger representiation of years
    */
    -(NSInteger)yearsUntil{
    return [self yearsLaterThan:[NSDate date]];
    }

    /**
    * Returns the number of months until the receiver's date. Returns 0 if the receiver is the same or earlier than now.
    *
    * @return NSInteger representiation of months
    */
    -(NSInteger)monthsUntil{
    return [self monthsLaterThan:[NSDate date]];
    }

    /**
    * Returns the number of weeks until the receiver's date. Returns 0 if the receiver is the same or earlier than now.
    *
    * @return NSInteger representiation of weeks
    */
    -(NSInteger)weeksUntil{
    return [self weeksLaterThan:[NSDate date]];
    }

    /**
    * Returns the number of days until the receiver's date. Returns 0 if the receiver is the same or earlier than now.
    *
    * @return NSInteger representiation of days
    */
    -(NSInteger)daysUntil{
    return [self daysLaterThan:[NSDate date]];
    }

    /**
    * Returns the number of hours until the receiver's date. Returns 0 if the receiver is the same or earlier than now.
    *
    * @return double representiation of hours
    */
    -(double)hoursUntil{
    return [self hoursLaterThan:[NSDate date]];
    }

    /**
    * Returns the number of minutes until the receiver's date. Returns 0 if the receiver is the same or earlier than now.
    *
    * @return double representiation of minutes
    */
    -(double)minutesUntil{
    return [self minutesLaterThan:[NSDate date]];
    }

    /**
    * Returns the number of seconds until the receiver's date. Returns 0 if the receiver is the same or earlier than now.
    *
    * @return double representiation of seconds
    */
    -(double)secondsUntil{
    return [self secondsLaterThan:[NSDate date]];
    }

    #pragma mark Time Ago
    /**
    * Returns the number of years the receiver's date is earlier than now. Returns 0 if the receiver is the same or later than now.
    *
    * @return NSInteger representiation of years
    */
    -(NSInteger)yearsAgo{
    return [self yearsEarlierThan:[NSDate date]];
    }

    /**
    * Returns the number of months the receiver's date is earlier than now. Returns 0 if the receiver is the same or later than now.
    *
    * @return NSInteger representiation of months
    */
    -(NSInteger)monthsAgo{
    return [self monthsEarlierThan:[NSDate date]];
    }

    /**
    * Returns the number of weeks the receiver's date is earlier than now. Returns 0 if the receiver is the same or later than now.
    *
    * @return NSInteger representiation of weeks
    */
    -(NSInteger)weeksAgo{
    return [self weeksEarlierThan:[NSDate date]];
    }

    /**
    * Returns the number of days the receiver's date is earlier than now. Returns 0 if the receiver is the same or later than now.
    *
    * @return NSInteger representiation of days
    */
    -(NSInteger)daysAgo{
    return [self daysEarlierThan:[NSDate date]];
    }

    /**
    * Returns the number of hours the receiver's date is earlier than now. Returns 0 if the receiver is the same or later than now.
    *
    * @return double representiation of hours
    */
    -(double)hoursAgo{
    return [self hoursEarlierThan:[NSDate date]];
    }

    /**
    * Returns the number of minutes the receiver's date is earlier than now. Returns 0 if the receiver is the same or later than now.
    *
    * @return double representiation of minutes
    */
    -(double)minutesAgo{
    return [self minutesEarlierThan:[NSDate date]];
    }

    /**
    * Returns the number of seconds the receiver's date is earlier than now. Returns 0 if the receiver is the same or later than now.
    *
    * @return double representiation of seconds
    */
    -(double)secondsAgo{
    return [self secondsEarlierThan:[NSDate date]];
    }

    #pragma mark Earlier Than
    /**
    * Returns the number of years the receiver's date is earlier than the provided comparison date.
    * Returns 0 if the receiver's date is later than or equal to the provided comparison date.
    *
    * @param date NSDate - Provided date for comparison
    *
    * @return NSInteger representing the number of years
    */
    -(NSInteger)yearsEarlierThan:(NSDate *)date{
    return ABS(MIN([self yearsFrom:date], 0));
    }

    /**
    * Returns the number of months the receiver's date is earlier than the provided comparison date.
    * Returns 0 if the receiver's date is later than or equal to the provided comparison date.
    *
    * @param date NSDate - Provided date for comparison
    *
    * @return NSInteger representing the number of months
    */
    -(NSInteger)monthsEarlierThan:(NSDate *)date{
    return ABS(MIN([self monthsFrom:date], 0));
    }

    /**
    * Returns the number of weeks the receiver's date is earlier than the provided comparison date.
    * Returns 0 if the receiver's date is later than or equal to the provided comparison date.
    *
    * @param date NSDate - Provided date for comparison
    *
    * @return NSInteger representing the number of weeks
    */
    -(NSInteger)weeksEarlierThan:(NSDate *)date{
    return ABS(MIN([self weeksFrom:date], 0));
    }

    /**
    * Returns the number of days the receiver's date is earlier than the provided comparison date.
    * Returns 0 if the receiver's date is later than or equal to the provided comparison date.
    *
    * @param date NSDate - Provided date for comparison
    *
    * @return NSInteger representing the number of days
    */
    -(NSInteger)daysEarlierThan:(NSDate *)date{
    return ABS(MIN([self daysFrom:date], 0));
    }

    /**
    * Returns the number of hours the receiver's date is earlier than the provided comparison date.
    * Returns 0 if the receiver's date is later than or equal to the provided comparison date.
    *
    * @param date NSDate - Provided date for comparison
    *
    * @return double representing the number of hours
    */
    -(double)hoursEarlierThan:(NSDate *)date{
    return ABS(MIN([self hoursFrom:date], 0));
    }

    /**
    * Returns the number of minutes the receiver's date is earlier than the provided comparison date.
    * Returns 0 if the receiver's date is later than or equal to the provided comparison date.
    *
    * @param date NSDate - Provided date for comparison
    *
    * @return double representing the number of minutes
    */
    -(double)minutesEarlierThan:(NSDate *)date{
    return ABS(MIN([self minutesFrom:date], 0));
    }

    /**
    * Returns the number of seconds the receiver's date is earlier than the provided comparison date.
    * Returns 0 if the receiver's date is later than or equal to the provided comparison date.
    *
    * @param date NSDate - Provided date for comparison
    *
    * @return double representing the number of seconds
    */
    -(double)secondsEarlierThan:(NSDate *)date{
    return ABS(MIN([self secondsFrom:date], 0));
    }

    #pragma mark Later Than
    /**
    * Returns the number of years the receiver's date is later than the provided comparison date.
    * Returns 0 if the receiver's date is earlier than or equal to the provided comparison date.
    *
    * @param date NSDate - Provided date for comparison
    *
    * @return NSInteger representing the number of years
    */
    -(NSInteger)yearsLaterThan:(NSDate *)date{
    return MAX([self yearsFrom:date], 0);
    }

    /**
    * Returns the number of months the receiver's date is later than the provided comparison date.
    * Returns 0 if the receiver's date is earlier than or equal to the provided comparison date.
    *
    * @param date NSDate - Provided date for comparison
    *
    * @return NSInteger representing the number of months
    */
    -(NSInteger)monthsLaterThan:(NSDate *)date{
    return MAX([self monthsFrom:date], 0);
    }

    /**
    * Returns the number of weeks the receiver's date is later than the provided comparison date.
    * Returns 0 if the receiver's date is earlier than or equal to the provided comparison date.
    *
    * @param date NSDate - Provided date for comparison
    *
    * @return NSInteger representing the number of weeks
    */
    -(NSInteger)weeksLaterThan:(NSDate *)date{
    return MAX([self weeksFrom:date], 0);
    }

    /**
    * Returns the number of days the receiver's date is later than the provided comparison date.
    * Returns 0 if the receiver's date is earlier than or equal to the provided comparison date.
    *
    * @param date NSDate - Provided date for comparison
    *
    * @return NSInteger representing the number of days
    */
    -(NSInteger)daysLaterThan:(NSDate *)date{
    return MAX([self daysFrom:date], 0);
    }

    /**
    * Returns the number of hours the receiver's date is later than the provided comparison date.
    * Returns 0 if the receiver's date is earlier than or equal to the provided comparison date.
    *
    * @param date NSDate - Provided date for comparison
    *
    * @return double representing the number of hours
    */
    -(double)hoursLaterThan:(NSDate *)date{
    return MAX([self hoursFrom:date], 0);
    }

    /**
    * Returns the number of minutes the receiver's date is later than the provided comparison date.
    * Returns 0 if the receiver's date is earlier than or equal to the provided comparison date.
    *
    * @param date NSDate - Provided date for comparison
    *
    * @return double representing the number of minutes
    */
    -(double)minutesLaterThan:(NSDate *)date{
    return MAX([self minutesFrom:date], 0);
    }

    /**
    * Returns the number of seconds the receiver's date is later than the provided comparison date.
    * Returns 0 if the receiver's date is earlier than or equal to the provided comparison date.
    *
    * @param date NSDate - Provided date for comparison
    *
    * @return double representing the number of seconds
    */
    -(double)secondsLaterThan:(NSDate *)date{
    return MAX([self secondsFrom:date], 0);
    }

    #pragma mark Comparators
    /**
    * Returns a YES if receiver is earlier than provided comparison date, otherwise returns NO
    *
    * @param date NSDate - Provided date for comparison
    *
    * @return BOOL representing comparison result
    */
    -(BOOL)isEarlierThan:(NSDate *)date{
    if (self.timeIntervalSince1970 < date.timeIntervalSince1970) {
    return YES;
    }
    return NO;
    }

    /**
    * Returns a YES if receiver is later than provided comparison date, otherwise returns NO
    *
    * @param date NSDate - Provided date for comparison
    *
    * @return BOOL representing comparison result
    */
    -(BOOL)isLaterThan:(NSDate *)date{
    if (self.timeIntervalSince1970 > date.timeIntervalSince1970) {
    return YES;
    }
    return NO;
    }

    /**
    * Returns a YES if receiver is earlier than or equal to the provided comparison date, otherwise returns NO
    *
    * @param date NSDate - Provided date for comparison
    *
    * @return BOOL representing comparison result
    */
    -(BOOL)isEarlierThanOrEqualTo:(NSDate *)date{
    if (self.timeIntervalSince1970 <= date.timeIntervalSince1970) {
    return YES;
    }
    return NO;
    }

    /**
    * Returns a YES if receiver is later than or equal to provided comparison date, otherwise returns NO
    *
    * @param date NSDate - Provided date for comparison
    *
    * @return BOOL representing comparison result
    */
    -(BOOL)isLaterThanOrEqualTo:(NSDate *)date{
    if (self.timeIntervalSince1970 >= date.timeIntervalSince1970) {
    return YES;
    }
    return NO;
    }

    #pragma mark - Formatted Dates
    #pragma mark Formatted With Style
    /**
    * Convenience method that returns a formatted string representing the receiver's date formatted to a given style
    *
    * @param style NSDateFormatterStyle - Desired date formatting style
    *
    * @return NSString representing the formatted date string
    */
    -(NSString *)formattedDateWithStyle:(NSDateFormatterStyle)style{
    return [self formattedDateWithStyle:style timeZone:[NSTimeZone systemTimeZone] locale:[NSLocale autoupdatingCurrentLocale]];
    }

    /**
    * Convenience method that returns a formatted string representing the receiver's date formatted to a given style and time zone
    *
    * @param style NSDateFormatterStyle - Desired date formatting style
    * @param timeZone NSTimeZone - Desired time zone
    *
    * @return NSString representing the formatted date string
    */
    -(NSString *)formattedDateWithStyle:(NSDateFormatterStyle)style timeZone:(NSTimeZone *)timeZone{
    return [self formattedDateWithStyle:style timeZone:timeZone locale:[NSLocale autoupdatingCurrentLocale]];
    }

    /**
    * Convenience method that returns a formatted string representing the receiver's date formatted to a given style and locale
    *
    * @param style NSDateFormatterStyle - Desired date formatting style
    * @param locale NSLocale - Desired locale
    *
    * @return NSString representing the formatted date string
    */
    -(NSString *)formattedDateWithStyle:(NSDateFormatterStyle)style locale:(NSLocale *)locale{
    return [self formattedDateWithStyle:style timeZone:[NSTimeZone systemTimeZone] locale:locale];
    }

    /**
    * Convenience method that returns a formatted string representing the receiver's date formatted to a given style, time zone and locale
    *
    * @param style NSDateFormatterStyle - Desired date formatting style
    * @param timeZone NSTimeZone - Desired time zone
    * @param locale NSLocale - Desired locale
    *
    * @return NSString representing the formatted date string
    */
    -(NSString *)formattedDateWithStyle:(NSDateFormatterStyle)style timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale{
    static NSDateFormatter *formatter = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    formatter = [[NSDateFormatter alloc] init];
    });

    [formatter setDateStyle:style];
    [formatter setTimeZone:timeZone];
    [formatter setLocale:locale];
    return [formatter stringFromDate:self];
    }

    #pragma mark Formatted With Format
    /**
    * Convenience method that returns a formatted string representing the receiver's date formatted to a given date format
    *
    * @param format NSString - String representing the desired date format
    *
    * @return NSString representing the formatted date string
    */
    -(NSString *)formattedDateWithFormat:(NSString *)format{
    return [self formattedDateWithFormat:format timeZone:[NSTimeZone systemTimeZone] locale:[NSLocale autoupdatingCurrentLocale]];
    }

    /**
    * Convenience method that returns a formatted string representing the receiver's date formatted to a given date format and time zone
    *
    * @param format NSString - String representing the desired date format
    * @param timeZone NSTimeZone - Desired time zone
    *
    * @return NSString representing the formatted date string
    */
    -(NSString *)formattedDateWithFormat:(NSString *)format timeZone:(NSTimeZone *)timeZone{
    return [self formattedDateWithFormat:format timeZone:timeZone locale:[NSLocale autoupdatingCurrentLocale]];
    }

    /**
    * Convenience method that returns a formatted string representing the receiver's date formatted to a given date format and locale
    *
    * @param format NSString - String representing the desired date format
    * @param locale NSLocale - Desired locale
    *
    * @return NSString representing the formatted date string
    */
    -(NSString *)formattedDateWithFormat:(NSString *)format locale:(NSLocale *)locale{
    return [self formattedDateWithFormat:format timeZone:[NSTimeZone systemTimeZone] locale:locale];
    }

    /**
    * Convenience method that returns a formatted string representing the receiver's date formatted to a given date format, time zone and locale
    *
    * @param format NSString - String representing the desired date format
    * @param timeZone NSTimeZone - Desired time zone
    * @param locale NSLocale - Desired locale
    *
    * @return NSString representing the formatted date string
    */
    -(NSString *)formattedDateWithFormat:(NSString *)format timeZone:(NSTimeZone *)timeZone locale:(NSLocale *)locale{
    static NSDateFormatter *formatter = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    formatter = [[NSDateFormatter alloc] init];
    });

    [formatter setDateFormat:format];
    [formatter setTimeZone:timeZone];
    [formatter setLocale:locale];
    return [formatter stringFromDate:self];
    }

    #pragma mark - Helpers
    /**
    * Class method that returns whether the given year is a leap year for the Gregorian Calendar
    * Returns YES if year is a leap year, otherwise returns NO
    *
    * @param year NSInteger - Year to evaluate
    *
    * @return BOOL evaluation of year
    */
    +(BOOL)isLeapYear:(NSInteger)year{
    if (year*400){
    return YES;
    }
    else if (year%100){
    return NO;
    }
    else if (year%4){
    return YES;
    }

    return NO;
    }

    /**
    * Retrieves the default calendar identifier used for all non-calendar-specified operations
    *
    * @return NSString - NSCalendarIdentifier
    */
    +(NSString *)defaultCalendarIdentifier {
    return defaultCalendarIdentifier;
    }

    /**
    * Sets the default calendar identifier used for all non-calendar-specified operations
    *
    * @param identifier NSString - NSCalendarIdentifier
    */
    + (void)setDefaultCalendarIdentifier:(NSString *)identifier {
    defaultCalendarIdentifier = [identifier copy];
    implicitCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:defaultCalendarIdentifier ?: NSGregorianCalendar];
    }

    /**
    * Retrieves a default NSCalendar instance, based on the value of defaultCalendarSetting
    *
    * @return NSCalendar The current implicit calendar
    */
    + (NSCalendar *)implicitCalendar {
    return implicitCalendar;
    }

    @end

######3. 时间显示格式

格式 显示
dd/MM/yyyy hh:mm a 30/03/2015 4:00 PM
MMM dd,YYYY Apr 11,2015
EEE MMM dd HH:mm:ss Z yyyy Thu Apr 09 14:37:22 +0800 2015

#####4. 时间工具方法

  1. 从 NSString -> NSDate
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    + (NSDate *)getDateFromString:(NSString *)pstrDate format:(NSString *)aFormat
    {
    if (pstrDate.length == 0)
    {
    return nil;
    }

    NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
    [df1 setDateFormat:aFormat];
    NSDate *dtPostDate = [df1 dateFromString:pstrDate];
    return dtPostDate;
    }

效果图

(无)

备注

(无)