ios 如何在 dd/MM/YYYY 中格式化 NSDate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18590387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:57:26  来源:igfitidea点击:

How to format a NSDate in dd/MM/YYYY

iosobjective-cnsstringnsdate

提问by user2588945

I'm trying to get an NSDate into the format: 20/05/2014 22:30

我正在尝试将 NSDate 转换为以下格式:20/05/2014 22:30

here's the code I currently have. [dateFormat setDateFormat:@"YYYY-MM-dd\'T\'HH:mm:ssZZZZZ"];this works but the format is not like 20/05/2014 22:30it displays the date in format like: 12-03-2013 12:00:00 +00:00:00

这是我目前拥有的代码。 [dateFormat setDateFormat:@"YYYY-MM-dd\'T\'HH:mm:ssZZZZZ"];这有效,但格式不像20/05/2014 22:30它以如下格式显示日期:12-03-2013 12:00:00 +00:00:00

and then when I use:

然后当我使用:

[dateFormat setDateFormat:@"MM/dd/yyyy"];I get a nullvalue returned instead of a formatted date.

[dateFormat setDateFormat:@"MM/dd/yyyy"];我得到一个null返回的值而不是格式化的日期。

// timestamp conversion
    NSString *str = [timeStamp objectAtIndex:indexPath.row];
    // convert to date
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
  //  [dateFormat setDateFormat:@"YYYY-MM-dd\'T\'HH:mm:ssZZZZZ"];

 [dateFormat setDateFormat:@"MM/dd/yyyy"];

    NSDate *dte = [dateFormat dateFromString:str];
    cell.timeStampLabel.text = [NSString stringWithFormat:@"%@",dte ];

the str original string output before formatting is str 2013-08-23T15:21:19+02:00

格式化前的 str 原始字符串输出是 str 2013-08-23T15:21:19+02:00

thanks for any help

谢谢你的帮助

回答by Pratik

you have to do like this but please first of check your str's date format

你必须这样做,但请先检查你的 str 的日期格式

NSString *str = [timeStamp objectAtIndex:indexPath.row];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd\'T\'HH:mm:ssZZZZZ"];


NSDate *dte = [dateFormat dateFromString:str];

[dateFormat setDateFormat:@"dd/MM/yyyy HH:mm"];
cell.timeStampLabel.text = [NSString stringWithFormat:@"%@",[dateFormat stringFromDate:dte]];

first time set date format same as in your str.

第一次设置日期格式与您的 str 相同。

回答by iEinstein

First set the NSDateFormatterto the format you are receiving data to retain it into a NSDate Object and then again use your desired format to change it into the format you want and show it. For more you can see Pratikanswer.

首先将 设置为NSDateFormatter您正在接收数据的格式以将其保留到 NSDate 对象中,然后再次使用您想要的格式将其更改为您想要的格式并显示它。有关更多信息,您可以查看Pratik 的回答。

Hope this helps.

希望这可以帮助。

回答by PeterParker

set your date formate in [dateFormat setDateFormat:@"dd/MM/yyyy HH:mm"]; so that u code write in way

在 [dateFormat setDateFormat:@"dd/MM/yyyy HH:mm"] 中设置您的日期格式;所以你的代码写的方式

// timestamp conversion
NSString *str = [timeStamp objectAtIndex:indexPath.row];
// convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
//  [dateFormat setDateFormat:@"YYYY-MM-dd\'T\'HH:mm:ssZZZZZ"];

[dateFormat setDateFormat:@"dd/MM/yyyy HH:mm"];

NSDate *dte = [dateFormat dateFromString:str];
cell.timeStampLabel.text = [NSString stringWithFormat:@"%@",dte ];

I hope it is helpful.

我希望它有帮助。

回答by Rachit

Just use below code and you will get the result:

只需使用下面的代码,你就会得到结果:

// timestamp conversion

// 时间戳转换

NSString *str = [timeStamp objectAtIndex:indexPath.row];

// convert to date

// 转换为日期

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd T HH:mm:ss ZZZZ"];

NSDate *dte = [dateFormat dateFromString:str];

[dateFormat setDateFormat:@"dd/MM/yyyy HH:mm"];

cell.timeStampLabel.text = [NSString stringWithFormat:@"%@",[dateFormat stringFromDate:dte]];

回答by Pétur Ingi Egilsson

I think that enforcing the locale` and its appearance programatically is a bad idea unless it is has been specified as a requirement.

我认为以编程方式强制执行 locale` 及其外观是一个坏主意,除非它已被指定为要求。

An alternative would be to use setDateStyleon the NSDateFormatter, to one of the formats specified under System Preferences -> Language & Text -> Region -> Dates. There is a Short , Medium and a Long format to choose from for both Time and Date. Available styles are listed in the documentation under NSDateFormatterStyle

另一种方法是使用setDateStyleNSDateFormatter,对的下指定的格式中的一种System Preferences -> Language & Text -> Region -> Dates。时间和日期都有短格式、中格式和长格式可供选择。可用样式列在NSDateFormatterStyle下的文档中

Now to answer your question: If you would append the short timeformat to the medium dateformat then you would get your desired outcome, while maintaining both localizability and customisability.

现在回答您的问题:如果您将短时间格式附加到中日期格式,那么您将获得所需的结果,同时保持可本地化和可定制性。

回答by Toseef Khilji

Set Your date formatter to

将您的日期格式化程序设置为

 [dateFormat setDateFormat:@"dd/MM/yyyy HH:mm"];

// timestamp conversion

// 时间戳转换

NSString *str = [timeStamp objectAtIndex:indexPath.row];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy HH:mm"];

NSDate *dte = [dateFormat dateFromString:str];
cell.timeStampLabel.text = [NSString stringWithFormat:@"%@",dte ];

回答by Gobi M

Have a look at DateFormatters to use required formats: Apple Doc

查看 DateFormatters 以使用所需的格式: Apple Doc

回答by Kalpesh Satasiya

**NSString *strNotificationTimeAndDate="Your date";

**NSString *strNotificationTimeAndDate="你的日期";

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss.z"];

or

或者

[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss+z"];

or

或者

[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ssz"];

or

或者

[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
NSDate *commentdate = [dateFormat 
dateFromString:strNotificationTimeAndDate];**