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

时间:2023-02-09 20:24:21

I basically need to get current date and time separately, formatted as:

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

2009-04-26 
11:06:54

The code below, from another question on the same topic, generates

生成以下代码,来自同一主题的另一个问题

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

This is almost what I'm looking for, but I want to separate the day and time.

这几乎是我正在寻找的,但我想分开日期和时间。

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

8 个解决方案

#1


iPhone format strings are in Unicode format. Behind the link is a table explaining what all the letters above mean so you can build your own.

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

And of course don't forget to release your date formatters when you're done with them. The above code leaks format, now, and inFormat.

当然,当你完成它们时,不要忘记发布日期格式化程序。上面的代码泄漏了格式,现在和inFormat。

#2


this is what i used:

这是我用过的:

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

#3


you can use this method just pass your date to it

您可以使用此方法只是将您的日期传递给它

-(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


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

set the format to return is....

设置返回的格式是....

yyyy-MM-dd return 2015-12-17 date

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

yyyy-MMM-dd return 2015-Dec-17 date

yyyy-MMM-dd返回2015年12月17日

yy-MM-dd return 15-12-17 date

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

dd-MM-yy return 17-12-15 date

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

dd-MM-yyyy return 17-12-2015 date

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

yyyy-MMM-dd HH:mm:ss return 2015-Dec-17 08:07:13 date and time

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

yyyy-MMM-dd HH:mm return 2015-Dec-17 08:07 date and time

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

For more Details Data and Time Format for Click Now.

有关立即单击的更多详细信息数据和时间格式。

Thank you.....

#5


nothing new but still want to share my method:

什么都不新但仍想分享我的方法:

+(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;
}

#6


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

#7


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

}

Usage

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

Play with template to match your needs. Examples and doc here to help you build the template you need.

玩模板以满足您的需求。这里的示例和文档可帮助您构建所需的模板。

Note

You may want to cache your DateFormatter if you plan to use it in TableView for instance. To give an idea, looping over 1000 dates took me 0.5 sec using the above toString(template: String) function, compared to 0.05 sec using myFormatter.string(from: Date).

如果您计划在TableView中使用它,则可能需要缓存DateFormatter。为了给出一个想法,使用上面的toString(template:String)函数循环超过1000个日期花了我0.5秒,而使用myFormatter.string(from:Date)则为0.05秒。

#8


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

Thats it, you got it all you want.

多数民众赞成,你得到了你想要的一切。

#1


iPhone format strings are in Unicode format. Behind the link is a table explaining what all the letters above mean so you can build your own.

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

And of course don't forget to release your date formatters when you're done with them. The above code leaks format, now, and inFormat.

当然,当你完成它们时,不要忘记发布日期格式化程序。上面的代码泄漏了格式,现在和inFormat。

#2


this is what i used:

这是我用过的:

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

#3


you can use this method just pass your date to it

您可以使用此方法只是将您的日期传递给它

-(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


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

set the format to return is....

设置返回的格式是....

yyyy-MM-dd return 2015-12-17 date

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

yyyy-MMM-dd return 2015-Dec-17 date

yyyy-MMM-dd返回2015年12月17日

yy-MM-dd return 15-12-17 date

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

dd-MM-yy return 17-12-15 date

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

dd-MM-yyyy return 17-12-2015 date

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

yyyy-MMM-dd HH:mm:ss return 2015-Dec-17 08:07:13 date and time

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

yyyy-MMM-dd HH:mm return 2015-Dec-17 08:07 date and time

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

For more Details Data and Time Format for Click Now.

有关立即单击的更多详细信息数据和时间格式。

Thank you.....

#5


nothing new but still want to share my method:

什么都不新但仍想分享我的方法:

+(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;
}

#6


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

#7


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

}

Usage

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

Play with template to match your needs. Examples and doc here to help you build the template you need.

玩模板以满足您的需求。这里的示例和文档可帮助您构建所需的模板。

Note

You may want to cache your DateFormatter if you plan to use it in TableView for instance. To give an idea, looping over 1000 dates took me 0.5 sec using the above toString(template: String) function, compared to 0.05 sec using myFormatter.string(from: Date).

如果您计划在TableView中使用它,则可能需要缓存DateFormatter。为了给出一个想法,使用上面的toString(template:String)函数循环超过1000个日期花了我0.5秒,而使用myFormatter.string(from:Date)则为0.05秒。

#8


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

Thats it, you got it all you want.

多数民众赞成,你得到了你想要的一切。