iphone - EDT 到 GMT 转换

标签 iphone ios objective-c

输入:美国东部时间 2013 年 4 月 26 日星期五 21:56:31

 NSDateFormatter *df1 = [[NSDateFormatter alloc] init];

 [df1 setDateFormat:@"EE, dd MMM YYYY HH:mm:ss zzz"];

  NSDateFormatter *df = [[NSDateFormatter alloc] init];

 [df setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];

 NSString *startDateStr2 = [df1 stringFromDate:[NSDate date]];

 NSLog(@"currentDate: %@", startDateStr2);

但我得到输出: Sat, 05 Jan 2013 07:26:31 GMT+05:30

最佳答案

使用下面的方法来改变NSDate的格式..

-(NSString *)changeDateFormat:(NSString*)stringDate dateFormat:(NSString*)dateFormat getwithFormat:(NSString *)getwithFormat{


    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:dateFormat];

    NSDate *date = [dateFormatter dateFromString:stringDate];

    dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:getwithFormat];

    NSString *convertedString = [dateFormatter stringFromDate:date];
    NSLog(@"Converted String : %@",convertedString);
    return convertedString;
}

用它的 3 个参数调用上面的方法..

<强>1。 stringDate : 以字符串类型传递你的日期。

<强>2。 dateFormat : 设置在您传递的字符串日期中使用的格式。例如,如果您的字符串日期是 "14-03-2013 11:22 am",则将格式设置为 @"dd-MM-yyyy hh:mm a" .

<强>3。 getwithFormat : 设置你想要的格式,就好像你想要日期 14/03/2013 然后只设置格式 @"dd/MM/yyyy".

如何调用这个方法见下面的例子:

NSString *strSDate = [self changeDateFormat:@"14-03-2013 11:22 am" dateFormat:@"dd-MM-yyyy hh:mm a" getwithFormat:@"dd/MM/yyyy"];
NSLog(@"\n\n Now Date => %@",strSDate);

输出为:Now Date => 14/03/2013

希望对你有所帮助...

关于iphone - EDT 到 GMT 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16271399/

相关文章:

ios - 在我的 tableView 中看不到来自 JSON 的图像

ios - 父 View 在关闭模态视图 Controller 后更改其框架

ios - 定义验证字符串时出错

iphone - 如何在 iOS 上从 pdf 文件中提取给定页面?

iphone - 如何将 "transfer"第一响应者从一个 UIView 转移到另一个 UIView?

ios - 使用约束将按钮移向目标,没有反应?

iphone - 如何使用自定义 inputView 设计 iOS 程序

iphone - 在iPhone处于静音模式时如何使用AVAudioPlayer播放声音

ios - iOS 的 Interface Builder 中没有 IBOutlets

ios - iOS滚动后如何保持tableview的单元格颜色