0

在 Objective C/iPhone 中是否有与 PHP 的 strtotime 等效(或模糊相似)?

例如,strtotime 可以理解以下时间表达式(来自 php 文档):

  • echo strtotime("现在"), "\n";
  • echo strtotime("2000 年 9 月 10 日"), "\n";
  • echo strtotime("+1 天"), "\n";
  • echo strtotime("+1 周"), "\n";
  • echo strtotime("+1 周 2 天 4 小时 2 秒"), "\n";
  • echo strtotime("下周四"), "\n";
  • echo strtotime("上周一"), "\n";
4

3 回答 3

1

[NSDate +dateWithString]做你想做的,我想。

于 2010-08-05T06:01:14.483 回答
0

这是您的第一个和第二个查询的一些代码:

  • echo strtotime("现在"), "\n";
  • echo strtotime("2000 年 9 月 10 日"), "\n";
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [格式 setDateFormat:@"dd MMM yyyy"];
    NSDate *now = [[NSDate alloc] init];
    NSString *dateString = [格式 stringFromDate:now];
    NSLog(@"时间:%@", dateString);

    NSDate *parsed = [format dateFromString:@"10 Sep 2000"];
    NSLog(@"时间:%@", [format stringFromDate:parsed]);

显然分配的对象需要释放。

这是一篇可能对其他人有所帮助的文章:

http://www.drobnik.com/touch/2009/05/adding-days-to-nsdate

于 2010-08-05T09:03:41.733 回答
0

突然我想起了“PHPJS”项目。这里有一个 strtotime 的 javascript 端口:

http://phpjs.org/functions/strtotime:554

我还没有测试它(这意味着,将它移植到 Objective C 函数或类似函数),所以我不确定它与原始 strtotime 相比的实际性能如何。以后可能有时间检查一下...

于 2010-08-06T06:57:46.230 回答