将NSDate格式化为特定的年、月、日、小时、分钟和秒的样式

132

我需要单独获取当前日期和时间,并格式化为:

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);

1
在这种情况下,如果你花时间编写了解决问题所使用的代码,我可能会将其(在这种情况下是编辑2以下的所有内容)放在回答中,而不是编辑到你的帖子中。无论如何,这并不是什么大事,但它使得更容易找到/看到它是对问题的解决方案。 - Redwood
8个回答

194

这是我使用的代码:

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];

72

iPhone格式字符串采用Unicode格式。链接后面有一张表格,解释了以上所有字母的含义,以便您可以构建自己的格式。

当您完成使用日期格式化程序后,请不要忘记释放它们。上述代码泄漏了formatnowinFormat


即使启用ARC,它还是泄漏了吗? - dimme
19
以上内容在ARC下不会有任何泄漏问题(当然,当我们进行这个讨论时,ARC并不可用于2009年)。 - Rob Napier

12
    NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
    [dateformat setDateFormat:@"Your Date Format"];

设置返回的格式为...

yyyy-MM-dd 返回 2015-12-17 日期

yyyy-MMM-dd 返回 2015-Dec-17 日期

yy-MM-dd 返回 15-12-17 日期

dd-MM-yy 返回 17-12-15 日期

dd-MM-yyyy 返回 17-12-2015 日期

yyyy-MMM-dd HH:mm:ss 返回 2015-Dec-17 08:07:13 日期和时间

yyyy-MMM-dd HH:mm 返回 2015-Dec-17 08:07 日期和时间

有关更多详细的日期和时间格式,请点击此处

谢谢.....


1
请在代码示例中提供附加说明。 - Maciej Lach
我有这个日期 '2018-06-08T07:30:05.391Z',我尝试了 'yyyy-MM-dd'T'HH:mm:ss.000Z' 但是得到了 nil 值。 - Pramod Tapaniya

11

你可以使用这种方法,只需将你的日期传递给它即可。

-(NSString *)getDateFromString:(NSString *)string
{

    NSString * dateString = [NSString stringWithFormat: @"%@",string];

    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"your current date format"];
    NSDate* myDate = [dateFormatter dateFromString:dateString];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"your desired format"];
    NSString *stringFromDate = [formatter stringFromDate:myDate];

    NSLog(@"%@", stringFromDate);
    return stringFromDate;
}

4

虽然没有新内容,但我仍然想分享我的方法:

+(NSString*) getDateStringFromSrcFormat:(NSString *) srcFormat destFormat:(NSString *)
destFormat scrString:(NSString *) srcString
{
    NSString *dateString = srcString;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //[dateFormatter setDateFormat:@"MM-dd-yyyy"];
    [dateFormatter setDateFormat:srcFormat];
    NSDate *date = [dateFormatter dateFromString:dateString];

    // Convert date object into desired format
    //[dateFormatter setDateFormat:@"yyyy-MM-dd"];
    [dateFormatter setDateFormat:destFormat];
    NSString *newDateString = [dateFormatter stringFromDate:date];
    return newDateString;
}

3

对于Swift

var dateString:String = "2014-05-20";
var dateFmt = NSDateFormatter()
// the format you want
dateFmt.dateFormat = "yyyy-MM-dd"
var date1:NSDate = dateFmt.dateFromString(dateString)!;

0

Swift 3

extension Date {
    
    func toString(template: String) -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = DateFormatter.dateFormat(fromTemplate: template, options: 0, locale: NSLocale.current)
        return formatter.string(from: self)
    }
    
}

使用方法

let now = Date()
let nowStr0 = now.toString(template: "EEEEdMMM") // Tuesday, May 9
let nowStr1 = now.toString(template: "yyyy-MM-dd") // 2017-05-09
let nowStr2 = now.toString(template: "HH:mm:ss") // 17:47:09

根据您的需求调整模板。这里提供示例和文档here,以帮助您构建所需的模板。

注意

如果您计划在TableView中使用它,则可能需要缓存您的DateFormatter。 举个例子,使用上述toString(template: String)函数循环1000个日期花费了我0.5秒,而使用myFormatter.string(from: Date)只需要0.05秒


为了使那个注释更加强大(因为它非常重要),您可以重构函数,以带有一个可选参数,默认值为新实例。我不知道为什么你被踩了,所以我给你点赞。 - Nat

-5
NSDate *date         = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"]
NSString *dateString  = [df stringFromDate:date];
[df setDateFormat:@"hh:mm:ss"];
NSString *hoursString = [df stringFromDate:date];

就是这样,你得到了你想要的一切。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接