【问题标题】:NSDate Formatting individuallyNSDate 单独格式化
【发布时间】:2014-02-27 12:28:41
【问题描述】:

我正在使用 NSDateFormatter 来格式化 NSDate 变量,如下所示:

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"dd/MM";

我只想为一年的第一天提供一种独特的格式(setDateFormat:@"dd/MM/yyyy")。

有可能吗?

提前致谢。

【问题讨论】:

  • 一年的第一天总是 01.01 或者在你的情况下是 01/01,然后你可以添加年份或者你是什么意思?
  • 您必须使用NSDateComponentsNSDate 中提取日期,然后有条件地使用不同的格式。不太难。

标签: ios objective-c nsdate nsformatter


【解决方案1】:

你可以用这样的东西来格式化你的日期,

NSDate *dateInAction = [NSDate date]; //Your date here
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth fromDate:dateInAction];
NSInteger day = [components day];
NSInteger month = [components month];
if (day==1 && month==1) {
    // Set formatter for first day of year
}else{
    // all other days
}

【讨论】:

    猜你喜欢
    • 2011-09-17
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多