objective-c 字符串怎么转时间

兄弟姐妹们,请说下,objective-c 字符串怎么转时间
最新回答
给十年后的我

2020-09-22 17:27:20

NSDate类是用来表示时间的,它有一个函数dateWithNaturalLanguageString能从字符串形式的时间生成NSDate对象,但是这个函数好像在高版本不支持了。
用法:
NSDate* myDate = [NSDate dateWithNaturalLanguageString:@“3pm December 31, 2001,”];

下面是苹果网站的内容
链接地址:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/index.html#//apple_ref/occ/clm/NSDate/dateWithNaturalLanguageString:


Creates and returns an NSDate object set to the date and time specified by a given string.

Declaration

Swift

class func dateWithNaturalLanguageString(_ string: String) -> AnyObject?

Objective-C

+ (id)dateWithNaturalLanguageString:(NSString *)string

Parameters

string

A string that contains a colloquial specification of a date, such as
“last Tuesday at dinner,” “3pm December 31, 2001,” “12/31/01,” or
“31/12/01.”

Return Value

A new NSDate object set to the current date and time specified by string.

Discussion

This method supports only a limited set of colloquial phrases,
primarily in English. It may give unexpected results, and its use is
strongly discouraged. To create a date object from a string, you should
use a date formatter object instead (see NSDateFormatter and Data Formatting Guide).

In parsing the string, this method uses the date and time preferences stored in the user’s defaults database. (See dateWithNaturalLanguageString:locale: for a list of the specific items used.)

Import Statement

import Foundation

Availability

Available in OS X v10.4 and later.
Deprecated in OS X v10.10.
像风一样

2022-11-17 04:54:25

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:@"2010-08-04 16:01:03"];
NSLog(@"%@", date);
[dateFormatter release];
使用NSDate和NSString相互转换