NSDate获取年/月/日

本文翻译自:NSDate get year/month/day

How can I get the year/month/day of a NSDate object, given no other information? 在没有其他信息的情况下,如何获取NSDate对象的年/月/日? I realize that I could probably do this with something similar to this: 我意识到我可以用类似的方法做到这一点:

NSCalendar *cal = [[NSCalendar alloc] init];
NSDateComponents *components = [cal components:0 fromDate:date];
int year = [components year];
int month = [components month];
int day = [components day];

But that seems to be a whole lot of hassle for something as simple as getting a NSDate 's year/month/day. 但这似乎很麻烦,例如获取NSDate的年/月/日。 Is there any other solution? 还有其他解决方案吗?


#1楼

参考:https://stackoom.com/question/fvcD/NSDate获取年-月-日


#2楼

Just to reword Itai's excellent (and working!) code, here's what a sample helper class would look like, to return the year value of a given NSDate variable. 只是改写Itai的出色代码(并且可以正常工作!),这是示例帮助程序类的样子,以返回给定NSDate变量的年份值。

As you can see, it's easy enough to modify this code to get the month or day. 如您所见,修改此代码以获取月或日非常容易。

+(int)getYear:(NSDate*)date
{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:date];

    int year = [components year];
    int month = [components month];
    int day = [components day];

    return year;
}

(I can't believe we're having to write our own basic iOS date functions like this, in 2013...) (我不敢相信我们必须在2013年编写这样的基本iOS日期函数...)

One other thing: don't ever use < and > to compare two NSDate values. 另一件事:永远不要使用<和>比较两个NSDate值。

XCode will happily accept such code (without any errors or warnings), but its results are a lottery. XCode会很乐意接受这样的代码(没有任何错误或警告),但其结果是彩票。 You must use the "compare" function to compare NSDates: 必须使用“比较”功能来比较NSDate:

if ([date1 compare:date2] == NSOrderedDescending) {
    // date1 is greater than date2        
}

#3楼

Try the following: 请尝试以下操作:

    NSString *birthday = @"06/15/1977";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM/dd/yyyy"];
    NSDate *date = [formatter dateFromString:birthday];
    if(date!=nil) {
        NSInteger age = [date timeIntervalSinceNow]/31556926;
        NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
        NSInteger day = [components day];
        NSInteger month = [components month];
        NSInteger year = [components year];

        NSLog(@"Day:%d Month:%d Year:%d Age:%d",day,month,year,age);
    }
    [formatter release];

#4楼

    NSDate *currDate = [NSDate date];
    NSCalendar*       calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents* components = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:currDate];
    NSInteger         day = [components day];
    NSInteger         month = [components month];
    NSInteger         year = [components year];
    NSLog(@"%d/%d/%d", day, month, year);

#5楼

i do in this way .... 我以这种方式做....

NSDate * mydate = [NSDate date];

NSCalendar * mycalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSCalendarUnit units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;

NSDateComponents * myComponents  = [mycalendar components:units fromDate:mydate];

NSLog(@"%d-%d-%d",myComponents.day,myComponents.month,myComponents.year);

#6楼

Try this . 尝试这个 。 . .

Code snippet: 程式码片段:

 NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
 int year = [components year];
 int month = [components month];
 int day = [components day];

It gives current year, month, date 它给出了当前的年,月,日

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值