相关疑难解决方法(0)

我如何在swift中获得String当前月份:

我需要得到可能作为当月,但我做不到.我怎样才能做到这一点.

   let date = NSDate()
    let calendar = NSCalendar.currentCalendar()
    let components = calendar.components([.Day , .Month , .Year], fromDate: date)

    let year =  components.year
    let month = components.month
    let day = components.day
Run Code Online (Sandbox Code Playgroud)

我做了这个,但没有奏效.

nsdate nscalendar ios swift

27
推荐指数
3
解决办法
4万
查看次数

如何将NSDate转换为NSString?

我想将NSDate转换为NSString?怎么可能?

我尝试过这个,但它没有用(它产生异常):

   NSString *dateToString=[[NSString alloc] initWithFormat:(NSString *)dateObj];
Run Code Online (Sandbox Code Playgroud)

提前致谢.

iphone

7
推荐指数
1
解决办法
2万
查看次数

检测两个日期是否在同一日历日内

我的应用程序显示消息列表,每个消息都有一个创建日期.

我不想只显示日期,而是根据创建日期过去的时间显示不同的格式.

例如,这是我从日期创建日期字符串的方式:

NSDate *date = someMessage.creationDate;
NSTimeInterval timePassed = [[NSDate date] timeIntervalSinceDate:date];

if (timePassed < 60 ) { // less then one minute
  return @"now"
} else if (timePassed < 3600) { // less then one hour
  NSInteger minutes = (NSInteger) floor(timePassed / 60.0);
  return [NSString stringWithFormat:@"%d minutes ago", minutes];
}
Run Code Online (Sandbox Code Playgroud)

所以现在我想添加以下案例:

else if ("timePassed < 24 hours ago and within the same calendar day")
   // do something
} else if ("timePassed > 24 hours, or within previous calendar …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c nsdate nscalendar ios

6
推荐指数
2
解决办法
2541
查看次数

比较NSDate的某些组件?

我如何只比较2的年 - 月 - 日组件NSDates?

iphone comparison nsdate

5
推荐指数
1
解决办法
3577
查看次数

NSDate - 还有更好的东西吗?

NSDate/NSDateFormatter 等人似乎为常见任务占用了太多代码.

像DateJS for JavaScript 这样的iOS开发有什么更好的东西吗?

以下是常见NSDate用法的一些示例,其中规范解决方案可以至少两倍小:

  1. 从整数创建NSDate.解决方案:SO答案1
  2. 从NSDate获取年/月/日.解决方案:SO答案2
  3. SO上有更多这些常见场景.

iphone objective-c

1
推荐指数
1
解决办法
1585
查看次数

标签 统计

iphone ×4

nsdate ×3

ios ×2

nscalendar ×2

objective-c ×2

comparison ×1

swift ×1