需要在Objective-C iOS中将自定义时间戳转换为不同的格式 [英] Need to convert custom timestamp to different format in Objective-C iOS

查看:155
本文介绍了需要在Objective-C iOS中将自定义时间戳转换为不同的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串2012-06-04,很难将其转换为: 2012年6月4日





谢谢

解决方案

p>一种方法是使用 NSDateFormatter ,然后使用2012年6月4日格式将NSDate转换为字符串:

  NSString * input = @2012-06-04; 

NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat:@yyyy-MM-dd];
NSDate * date = [df dateFromString:input];
[df setDateFormat:@MMMM d,yyyy];

return [df stringFromDate:date];

格式字符串的语法在 UTS#35


I have a string "2012-06-04" and am having a hard time converting it to: June 4, 2012.

Is there a quick way to transform this? I come from a ruby world where you would convert everything to seconds and that back out to the format you need it. Is there a reference that shows how to do that?

Thanks

解决方案

One way to do so is to convert the string to an NSDate using the NSDateFormatter with the 2012-06-04 format, and then convert the NSDate back to a string using the June 4, 2012 format:

NSString* input = @"2012-06-04";

NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
NSDate* date = [df dateFromString:input];
[df setDateFormat:@"MMMM d, yyyy"];

return [df stringFromDate:date];

The format string's syntax is described in UTS #35.

这篇关于需要在Objective-C iOS中将自定义时间戳转换为不同的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆