【问题标题】:How to get NSDate in "dd/MM/yyyy" formate ios?如何以“dd/MM/yyyy”格式获取 SYSDate ios?
【发布时间】:2014-12-11 10:06:31
【问题描述】:

我正在使用 NSDate 我创建了一个函数,它以“dd/MM/yyyy”格式给出今天的日期,但 NSDate 总是显示“nil”值。我用谷歌搜索了很多,也看到了很多 stackoverflow 答案,但所有答案都返回 NSString 值而不是 NSDate 值,所以请指导我获取给定格式的 NSDate。

+(NSDate*)getCurrentDate{
     NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
     [DateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

     //Date print here
     NSLog(@"Utility CurrentDate: %@",[DateFormatter stringFromDate:[NSDate date]]);    
     NSString *dateString = [DateFormatter stringFromDate:[NSDate date]];
     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

     NSDateFormatter *newDateFormatter = [[NSDateFormatter alloc]init];
     [newDateFormatter setDateFormat:DATE_FORMAT];
     //Date print here
     NSLog(@"New Date: %@",[newDateFormatter stringFromDate:[NSDate date]]);
     NSDate *dateFromString = [newDateFormatter dateFromString:dateString];

     return dateFromString;

}

【问题讨论】:

    标签: ios objective-c iphone nsdate nsdateformatter


    【解决方案1】:

    问题来了

    NSDate *dateFromString = [newDateFormatter dateFromString:dateString];
    

    这里你的dateString2014-12-11 03:41:31,因此它没有被格式化。您应该更改 newDateFormatterDateFormatter 以根据您的要求进行转换。

    编辑:

    [DateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; 替换为[DateFormatter setDateFormat: DATE_FORMAT];

    但是您的返回日期将不是您想要的,因为您返回的是一个NSDate 对象。你应该返回一个格式化的字符串。

    【讨论】:

    • 感谢您的回答我已尝试并返回此输出: CurrentDate: 11/12/2014 New Date: 11/46/2014 Current Date: 2014-01-10 18:42:00 +0000 i想要返回 NSDate 对象而不是 NSString。
    • @nishitpatel:看我的编辑,已经提到过...... NSDate 将始终以固定模式存在。如果您想要自定义格式,那么 NSString 是唯一的方法。
    【解决方案2】:

    您完全误解了 NSDate 是什么以及做什么。 NSDate 是一个绝对时间点。它没有格式。它不能有格式。绝对不可能有“dd/MM/yyyy”格式或任何其他格式的 NSDate。

    当您想向用户显示日期时,您可以使用 NSDateFormatter 将 NSDate 转换为字符串。以您喜欢的任何格式。

    【讨论】:

    • 感谢您的回答,现在我理解了 NSDate 的概念。
    【解决方案3】:

    在这里查看Convert date from string 或者你可以使用这个方法来转换。

    这三个函数都可以用来格式化日期值..

    -(NSString*)getStringFromDate:(NSDate*)pDate withFormat:(NSString*)pDateFormat{
    NSDateFormatter *dtFormatter = [[NSDateFormatter alloc] init];
    [dtFormatter setDateFormat:pDateFormat];
    [dtFormatter setLocale:[NSLocale systemLocale]];
    return [dtFormatter stringFromDate:pDate];}
    
    -(NSDate*)getDateFromString:(NSString*)pStrDate withFormat:(NSString*)pDateFormat{
    NSDateFormatter *dtFormatter = [[NSDateFormatter alloc] init];
    [dtFormatter setDateFormat:pDateFormat];
    [dtFormatter setLocale:[NSLocale systemLocale]];
    return [dtFormatter dateFromString:pStrDate];}
    
    -(NSString*)dateStringFromString:(NSString *)dateToConver format:(NSString *)fromFormat toFormat:(NSString *)toFormat{
    NSDate *date = [Helper getDateFromString:dateToConver withFormat:fromFormat];
    return [Helper getStringFromDate:date withFormat:toFormat];}
    

    【讨论】:

      【解决方案4】:

      参考dd/MM/yyyy format中的以下修改方法:-

      +(NSString*)getCurrentDate{
          NSDateFormatter *format=[[NSDateFormatter alloc]init];
          [format setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
          NSString *currentDt=[format stringFromDate:[NSDate date]];
          NSDate *dt=[format dateFromString:currentDt];
          [format setDateFormat:@"dd/MM/yyy"];
          return [format stringFromDate:dt];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-09
        • 1970-01-01
        • 2015-11-18
        • 2016-01-02
        • 1970-01-01
        • 2011-12-06
        • 2012-12-09
        • 1970-01-01
        相关资源
        最近更新 更多