Swift:使用strftime和localtime进行NSDate格式化

如何将以下Objective-C代码转换为Swift代码?

#define MAX_SIZE 11

char buffer[MAX_SIZE];

time_t time = [[NSDate date] timeIntervalSince1970];

strftime(buffer, MAX_SIZE, "%-l:%M\u2008%p", localtime(&time));

NSString *dateString = [NSString stringWithUTF8String:buffer];

NSLog(@"dateString: %@", dateString); // dateString: 11:56 PM

我正在格式化date

回答:

    let maxSize: UInt = 11

var buffer: CChar[] = CChar[](count: Int(maxSize), repeatedValue: 0)

var time: time_t = Int(NSDate().timeIntervalSince1970)

var length = strftime(&buffer, maxSize, "%-l:%M\u2008%p", localtime(&time))

var dateString = NSString(bytes: buffer, length: Int(length), encoding: NSUTF8StringEncoding)

NSLog("dateString: %@", dateString) // dateString: 11:56 PM

以上是 Swift:使用strftime和localtime进行NSDate格式化 的全部内容, 来源链接: utcz.com/qa/411070.html

回到顶部