【问题标题】:Converting a string to an NSDate将字符串转换为 NSDate
【发布时间】:2011-01-19 15:57:35
【问题描述】:

如何在 iOS 上将字符串转换为 NSDate

【问题讨论】:

    标签: objective-c cocoa-touch nsstring formatting nsdate


    【解决方案1】:
    NSString *dateStr = @"20100223";
    
    // Convert string to date object
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyyMMdd"];
    NSDate *date = [dateFormat dateFromString:dateStr];  
    
    // Convert date object to desired output format
    [dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
    dateStr = [dateFormat stringFromDate:date];  
    [dateFormat release];
    

    希望这会对你有所帮助。

    【讨论】:

    【解决方案2】:

    你会想看看NSDateFormatter。确定日期字符串的格式,然后使用dateFromString:将字符串转换为NSDate对象。

    【讨论】:

      【解决方案3】:
      NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
      [dateFormat setDateFormat:@"dd/MM/YYYY"];
      NSDate *date = [dateFormat dateFromString:dateStr];  
      list = [self getYear:date];
      
      
      - (NSMutableArray *)getYear:(NSDate*)date
      {
          NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
      
          int year = [components year];
          int month = [components month];
          int day = [components day];
          NSLog(@"%d",year);
      
          NSMutableDictionary *dateDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%d", day], @"day", [NSString stringWithFormat:@"%d", month], @"month", [NSString stringWithFormat:@"%d", year], @"year", nil];
      
          return dateDict;
      }
      

      【讨论】:

      • 返回类型是NSMutableArray,但是你返回的NSMutableDictionary为什么?
      【解决方案4】:

      我在 Swift 中的工作版本

      func dateFor(timeStamp: String) -> NSDate
      {
          let formater = NSDateFormatter()
          formater.dateFormat = "HH:mm:ss:SSS - MMM dd, yyyy"
          return formater.dateFromString(timeStamp)!
      }
      
      
      func timeStampFor(date: NSDate) -> String
      {
          let dateFormatter = NSDateFormatter()
          dateFormatter.dateFormat = "HH:mm:ss:SSS - MMM dd, yyyy"
      
          return dateFormatter.stringFromDate(date)
      }
      

      【讨论】:

        【解决方案5】:

        一个simple approach,即使你有sqlite DB:

        // NSTimeInterval is basically typedef of double so you can receive it as double also.
        NSTimeInterval *current = [[NSDate date] timeIntervalSince1970]; 
        [DB addToDB:current];
        
        //retrieve the date from DB
        NSDate *retrievedDateItem = [[NSDate alloc] initWithTimeIntervalSince1970:[getDBValueAsDouble]]; //convert it from double (NSTimeInterval) to NSDate
        // Set the style
        [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
        [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
        // Converted to string
        NSString *convertedDate = [dateFormatter stringFromDate:retrievedDateItem];
        

        此示例的输出:

        2015 年 8 月 3 日下午 12:27:50

        【讨论】:

          【解决方案6】:
          +(NSDate*)str2date:(NSString*)dateStr{
              if ([dateStr isKindOfClass:[NSDate class]]) {
                  return (NSDate*)dateStr;
              }
          
              NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
              [dateFormat setDateFormat:@"yyyy-MM-dd"];
              NSDate *date = [dateFormat dateFromString:dateStr];
              return date;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多