首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将NSDate格式化为特定样式,用于年份、月份、日和小时、分钟、秒。

将NSDate格式化为特定样式,用于年份、月份、日和小时、分钟、秒。
EN

Stack Overflow用户
提问于 2018-03-23 01:01:56
回答 2查看 0关注 0票数 0

我基本上需要分别获得当前日期和时间,格式如下:

2009-04-26 
11:06:54

下面的代码从同一主题的另一个问题中生成

now:        |2009-06-01 23:18:23 +0100| 
dateString: |Jun 01, 2009 23:18| 
parsed:     |2009-06-01 23:18:00 +0100|

这几乎是我要找的,但我想把时间和时间分开。

NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"MMM dd, yyyy HH:mm"];

NSDate *now = [[NSDate alloc] init];

NSString *dateString = [format stringFromDate:now];

NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:@"MMM dd, yyyy"];

NSDate *parsed = [inFormat dateFromString:dateString];

NSLog(@"\n"
"now:        |%@| \n"
"dateString: |%@| \n"
"parsed:     |%@|", now, dateString, parsed);
EN

回答 2

Stack Overflow用户

发布于 2018-03-23 09:47:19

iPhone格式字符串在Unicode格式.。链接后面是一个表格,解释上面所有字母的含义,这样你就可以构建自己的字母了。

当然,当你完成约会时,别忘了释放你的约会表格。以上代码泄漏formatnow,和inFormat.

票数 0
EN

Stack Overflow用户

发布于 2018-03-23 10:58:04

这就是我所用的:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];

NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm:ss"];

NSDate *now = [[NSDate alloc] init];

NSString *theDate = [dateFormat stringFromDate:now];
NSString *theTime = [timeFormat stringFromDate:now];

NSLog(@"\n"
      "theDate: |%@| \n"
      "theTime: |%@| \n"
      , theDate, theTime);

[dateFormat release];
[timeFormat release];
[now release];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100007744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档