将@“dd-MM-yyyy HH:mm:SS”中保存的日期转换为格式为@“dd MM”为了显示方便

发布于 2024-12-23 11:21:18 字数 670 浏览 1 评论 0原文

我已经以@“dd-MM-yyyy HH:mm:SS”格式保存了日期,现在在数据库中它以指定的格式显示!但我有一个在查看提醒页面中显示日期的要求。因此我想以一种使日期的可见性在外观上简单的方式显示日期。

因此,我希望日期的格式为:1 月 23 日或 8 月 19 日等...

我已尝试以下代码,但它没有显示任何日期:

remin.Date = [[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statament, 3)]autorelease];

                NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
                [dateFormat setDateFormat:@"MMMM-dd"];
                NSDate *date = [dateFormat dateFromString:remin.Date];
                NSString *dateVal = [dateFormat stringFromDate:date];
                remin.Date = dateVal;

请通过您的宝贵建议提供解决方案,

提前致谢:)

I have saved a date in @"dd-MM-yyyy HH:mm:SS" format,Now in database it gets displayed in what ever format specified! But I have a requirement of displaying date in view reminder page.Hence I would like to display date in such a way that the visibility of date is simple in appearance.

Hence I would like to have the date with say the format:January 23 or August 19 etc...

I have tried the below code but its not displaying any date:

remin.Date = [[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statament, 3)]autorelease];

                NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
                [dateFormat setDateFormat:@"MMMM-dd"];
                NSDate *date = [dateFormat dateFromString:remin.Date];
                NSString *dateVal = [dateFormat stringFromDate:date];
                remin.Date = dateVal;

Please provide a solution through your valuable suggestions

Thanks in advance :)

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

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

发布评论

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

评论(4

℡寂寞咖啡 2024-12-30 11:21:18

尝试下面的代码

remin.Date = [[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statament, 3)]autorelease];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"dd-MM-yyyy HH:mm:SS"];
NSDate *date = [dateFormat dateFromString:remin.Date];
[dateFormat setDateFormat:@"MMMM dd"];
NSString *dateVal = [dateFormat stringFromDate:date];
remin.Date = dateVal;

Try the following code

remin.Date = [[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statament, 3)]autorelease];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"dd-MM-yyyy HH:mm:SS"];
NSDate *date = [dateFormat dateFromString:remin.Date];
[dateFormat setDateFormat:@"MMMM dd"];
NSString *dateVal = [dateFormat stringFromDate:date];
remin.Date = dateVal;
烦人精 2024-12-30 11:21:18

将您的代码更改为,

remin.Date = [[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statament, 3)]autorelease];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"dd-MM-yyyy HH:mm:SS"];
NSDate *date = [dateFormat dateFromString:remin.Date];
[dateFormat setDateFormat:@"dd-MM"];
NSString *dateVal = [dateFormat stringFromDate:date];
remin.Date = dateVal;

Change your code as,

remin.Date = [[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statament, 3)]autorelease];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"dd-MM-yyyy HH:mm:SS"];
NSDate *date = [dateFormat dateFromString:remin.Date];
[dateFormat setDateFormat:@"dd-MM"];
NSString *dateVal = [dateFormat stringFromDate:date];
remin.Date = dateVal;
勿挽旧人 2024-12-30 11:21:18

这是代码:-

NSString *dateString = @"15-08-2011 05:12:36";
NSDateFormatter *format = [[NSDateFormatter alloc]init];
[format setDateFormat:@"dd-MM-yyyy HH:mm:SS"];
NSDate *currentDate = [format dateFromString:dateString];

[format setDateFormat:@"dd MM"];
NSString *newDateString = [format stringFromDate:currentDate];

Here is the code:-

NSString *dateString = @"15-08-2011 05:12:36";
NSDateFormatter *format = [[NSDateFormatter alloc]init];
[format setDateFormat:@"dd-MM-yyyy HH:mm:SS"];
NSDate *currentDate = [format dateFromString:dateString];

[format setDateFormat:@"dd MM"];
NSString *newDateString = [format stringFromDate:currentDate];
剩一世无双 2024-12-30 11:21:18

在解析字符串之前,您需要告诉 NSDateFormatter 字符串的日期格式。

NSString *orginaleDate = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statament, 3)];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];

// Set the date format as stored in the date base
[dateFormat setDateFormat:@"dd-MM-yyyy HH:mm:SS"];
NSDate *date = [dateFormat dateFromString:remin.Date];

// set the date format for the representation 
[dateFormat setDateFormat:@"MMMM-dd"];
NSString *dateVal = [dateFormat stringFromDate:date];
remin.Date = dateVal;

[orginaleDate release], orginaleDate =nil;

您可能还想将时区存储在数据库中,但这取决于应用程序的要求。要添加时间,请使用此格式 dd-MM-yyyy HH:mm:SS z

You need to tell the NSDateFormatter the date format of the string, before you parse it.

NSString *orginaleDate = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statament, 3)];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];

// Set the date format as stored in the date base
[dateFormat setDateFormat:@"dd-MM-yyyy HH:mm:SS"];
NSDate *date = [dateFormat dateFromString:remin.Date];

// set the date format for the representation 
[dateFormat setDateFormat:@"MMMM-dd"];
NSString *dateVal = [dateFormat stringFromDate:date];
remin.Date = dateVal;

[orginaleDate release], orginaleDate =nil;

You might want to also store the time zone in the datebase, but this depends on the apps requirements. To add the time use this format dd-MM-yyyy HH:mm:SS z

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文