将NSDate转换为NSString
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将NSDate转换为NSString相关的知识,希望对你有一定的参考价值。
如何将NSDate
转换为NSString
,以便仅将@“ yyyy”]]格式的年份输出到字符串?
如何将NSDate转换为NSString,以便仅将@“ yyyy”格式的年份输出到字符串?] >> [[
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy"];
//Optionally for time zone conversions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];
NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance];
//unless ARC is active
[formatter release];
Swift 4.2:
func stringFromDate(_ date: Date) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "dd MMM yyyy HH:mm" //yyyy
return formatter.string(from: date)
}
我不知道我们怎么都错过了这个:NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterFullStyle]; NSLog(@"%@",dateString);
输出'13 / 06/12 00:22:39 GMT + 03:00'希望通过提供包括时间,年份,月份和日期在内的常规格式器来增加更多的价值。您可以使用此格式程序超过一年的时间
[dateFormat setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"];
网络上有很多NSDate
助手,我倾向于使用:下面的自述文件摘录:
NSString *displayString = [NSDate stringForDisplayFromDate:date];
这将产生以下几种输出:
‘3:42 AM’ – if the date is after midnight today ‘Tuesday’ – if the date is within the last seven days ‘Mar 1’ – if the date is within the current calendar year ‘Mar 1, 2008’ – else ;-)
在Swift中:
var formatter = NSDateFormatter() formatter.dateFormat = "yyyy" var dateString = formatter.stringFromDate(YourNSDateInstanceHERE)
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init]; [dateformate setDateFormat:@"yyyy"]; // Date formater NSString *date = [dateformate stringFromDate:[NSDate date]]; // Convert date to string NSLog(@"date :%@",date);
[如果您没有NSDate
-descriptionWithCalendarFormat:timeZone:locale:
(我不认为iPhone / Cocoa Touch包含此功能),则可能需要使用strftime和Monkey来配合一些C风格的字符串。您可以使用NSDate
从NSDate -timeIntervalSince1970
获取UNIX时间戳。+(NSString*)date2str:(NSDate*)myNSDateInstance onlyDate:(BOOL)onlyDate{ NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; if (onlyDate) { [formatter setDateFormat:@"yyyy-MM-dd"]; }else{ [formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"]; } //Optionally for time zone conversions // [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]]; NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance]; return stringFromDate; } +(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; }
只需添加此扩展名:
extension NSDate { var stringValue: String { let formatter = NSDateFormatter() formatter.dateFormat = "yourDateFormat" return formatter.stringFromDate(self) } }
如果您使用的是Mac OS X,则可以编写:
NSString* s = [[NSDate date] descriptionWithCalendarFormat:@"%Y_%m_%d_%H_%M_%S" timeZone:nil locale:nil];
但是此功能在ios上不可用。NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:myNSDateInstance]; NSInteger year = [components year]; // NSInteger month = [components month]; NSString *yearStr = [NSString stringWithFormat:@"%ld", year];
定义您自己的实用程序以格式化您的日期和所需的日期格式例如。
NSString * stringFromDate(NSDate *date) { NSDateFormatter *formatter [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"MM ∕ dd ∕ yyyy, hh꞉mm a"]; return [formatter stringFromDate:date]; }
快速格式:
func dateFormatterWithCalendar(calndarIdentifier: Calendar.Identifier, dateFormat: String) -> DateFormatter { let formatter = DateFormatter() formatter.calendar = Calendar(identifier: calndarIdentifier) formatter.dateFormat = dateFormat return formatter } //Usage let date = Date() let fotmatter = dateFormatterWithCalendar(calndarIdentifier: .gregorian, dateFormat: "yyyy") let dateString = fotmatter.string(from: date) print(dateString) //2018
swift 4的答案
static let dateformat: String = "yyyy-MM-dd'T'HH:mm:ss" public static func stringTodate(strDate : String) -> Date { let dateFormatter = DateFormatter() dateFormatter.dateFormat = dateformat let date = dateFormatter.date(from: strDate) return date! } public static func dateToString(inputdate : Date) -> String { let dateFormatter = DateFormatter() dateFormatter.dateFormat = dateformat return formatter.string(from: inputdate) }
NSString* s = [[NSDate date] descriptionWithCalendarFormat:@"%Y_%m_%d_%H_%M_%S" timeZone:nil locale:nil];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:myNSDateInstance];
NSInteger year = [components year];
// NSInteger month = [components month];
NSString *yearStr = [NSString stringWithFormat:@"%ld", year];
NSString * stringFromDate(NSDate *date)
{ NSDateFormatter *formatter
[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM ∕ dd ∕ yyyy, hh꞉mm a"];
return [formatter stringFromDate:date];
}
func dateFormatterWithCalendar(calndarIdentifier: Calendar.Identifier, dateFormat: String) -> DateFormatter {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: calndarIdentifier)
formatter.dateFormat = dateFormat
return formatter
}
//Usage
let date = Date()
let fotmatter = dateFormatterWithCalendar(calndarIdentifier: .gregorian, dateFormat: "yyyy")
let dateString = fotmatter.string(from: date)
print(dateString) //2018
static let dateformat: String = "yyyy-MM-dd'T'HH:mm:ss"
public static func stringTodate(strDate : String) -> Date
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = dateformat
let date = dateFormatter.date(from: strDate)
return date!
}
public static func dateToString(inputdate : Date) -> String
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = dateformat
return formatter.string(from: inputdate)
}
localizedStringFromDate:dateStyle:timeStyle:
[dateFormat setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"];
NSDate
助手,我倾向于使用:https://github.com/billymeltdown/nsdate-helper/
var formatter = NSDateFormatter()
formatter.dateFormat = "yyyy"
var dateString = formatter.stringFromDate(YourNSDateInstanceHERE)
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
[dateformate setDateFormat:@"yyyy"]; // Date formater
NSString *date = [dateformate stringFromDate:[NSDate date]]; // Convert date to string
NSLog(@"date :%@",date);
NSDate
-descriptionWithCalendarFormat:timeZone:locale:
(我不认为iPhone / Cocoa Touch包含此功能),则可能需要使用strftime和Monkey来配合一些C风格的字符串。您可以使用NSDate
从NSDate -timeIntervalSince1970
获取UNIX时间戳。+(NSString*)date2str:(NSDate*)myNSDateInstance onlyDate:(BOOL)onlyDate{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
if (onlyDate) {
[formatter setDateFormat:@"yyyy-MM-dd"];
}else{
[formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
}
//Optionally for time zone conversions
// [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];
NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance];
return stringFromDate;
}
+(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;
}
extension NSDate {
var stringValue: String {
let formatter = NSDateFormatter()
formatter.dateFormat = "yourDateFormat"
return formatter.stringFromDate(self)
}
}
以上是关于将NSDate转换为NSString的主要内容,如果未能解决你的问题,请参考以下文章
将(日期+时间)从 NSString 转换为 NSDate 并添加一些小时
如何将长整数时间转换为 NSString 或 NSDate 以显示时间?
使用字符串格式“Sun, 08 Jan 2012 13:57:38 +0000”将 NSString 转换为 NSDate