将字符串转换为 NSDate

发布于 2024-08-22 06:39:42 字数 45 浏览 2 评论 0原文

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

How is it possible to convert a string to an NSDate on iOS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

剩余の解释 2024-08-29 06:39:42
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];

希望这会对您有所帮助。

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];

Hope this will help you.

挥剑断情 2024-08-29 06:39:42

您需要查看 NSDateFormatter。确定日期字符串的格式,然后使用 dateFromString: 将该字符串转换为 NSDate 对象。

You'll want to take a look at NSDateFormatter. Determine the format of the date string and then use dateFromString: to convert the string to an NSDate object.

指尖上的星空 2024-08-29 06:39:42
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;
}
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;
}
你又不是我 2024-08-29 06:39:42

我的 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)
}

My working version in 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)
}
凡间太子 2024-08-29 06:39:42

简单的方法,即使您有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

A simple approach even if you have 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];

The output for this example:

Aug 3, 2015, 12:27:50 PM

折戟 2024-08-29 06:39:42
+(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;
}
+(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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文