将字符串转换为 NSDate

发布于 2024-11-08 22:49:03 字数 316 浏览 5 评论 0原文

我有一个“DateTimeArray”,该数组包含

DateTimeArray[index 0] = 20.05.2011 12:12:50    
DateTimeArray[index 1]=  20.05.2011 12:13:20   
DateTimeArray[index 2]=  20.05.2011 12:20:10  

字符串中的所有值,我想将此字符串转换为 NSDate 并且只想访问时间而不是日期
然后这次值将存储在数组中,这个新数组将用于绘制折线图

我希望有人知道这个问题

非常感谢

i have a "DateTimeArray" this array contain

DateTimeArray[index 0] = 20.05.2011 12:12:50    
DateTimeArray[index 1]=  20.05.2011 12:13:20   
DateTimeArray[index 2]=  20.05.2011 12:20:10  

all the value are in string,and i want to convert this string into NSDate and only want to access time not date
and then this time values will be stored in array and this newly array will be used for drawing line chart

i hope some one know this issue

thank you very much

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

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

发布评论

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

评论(6

顾忌 2024-11-15 22:49:04

试试这个

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm:ss"];
NSDate *formattedDate = [df dateFromString:[DateTimeArray objectAtIndex:requiredIndex]];
[df release];

Try this

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm:ss"];
NSDate *formattedDate = [df dateFromString:[DateTimeArray objectAtIndex:requiredIndex]];
[df release];
我只土不豪 2024-11-15 22:49:04

最佳和更简洁的方法:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd.MM.yyyy HH:mm:ss"];

for(int i=0; i < [DateTimeArray count]; ++i)
{
   NSString *string = [DateTimeArray objectAtIndex:i];
   NSDate *date = [df dateFromString:string];
   [timeArray addObject:time];
}

[df release]; // ALWAYS release unused objects

然后,为了仅访问几小时而不是几天,您应该选择每个 NSDate* 实例所需的组件:

// Get the Gregorian calendar
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

// Get the date
NSDate* date = [timeArray objectAtIndex:my_index];

// Get the hours, minutes, seconds
NSDateComponents* hour = [cal components:NSHourCalendarUnit fromDate:date];
NSDateComponents* minute = [cal components:NSMinuteCalendarUnit fromDate:date];
NSDateComponents* second = [cal components:NSSecondCalendarUnit fromDate:date];

Best and cleaner approach:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd.MM.yyyy HH:mm:ss"];

for(int i=0; i < [DateTimeArray count]; ++i)
{
   NSString *string = [DateTimeArray objectAtIndex:i];
   NSDate *date = [df dateFromString:string];
   [timeArray addObject:time];
}

[df release]; // ALWAYS release unused objects

Then to just access hours, and not days, you should select the required components of each NSDate* instance:

// Get the Gregorian calendar
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

// Get the date
NSDate* date = [timeArray objectAtIndex:my_index];

// Get the hours, minutes, seconds
NSDateComponents* hour = [cal components:NSHourCalendarUnit fromDate:date];
NSDateComponents* minute = [cal components:NSMinuteCalendarUnit fromDate:date];
NSDateComponents* second = [cal components:NSSecondCalendarUnit fromDate:date];
云归处 2024-11-15 22:49:04
    NSMutableArray *nsdateArray = [NSMutableArray arrayWithCapacity:[DateTimeArray count]];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd.MM.yyyy HH:mm:ss"];

    for(NSString *currentString in DateTimeArray){
        NSDate *date = [dateFormat dateFromString:currentString];
        [nsdateArray addObject:date];
    }
    [dateFormat release];
    NSLog(@"ArrayWithDate:%@",[nsdateArray description]);
    NSMutableArray *nsdateArray = [NSMutableArray arrayWithCapacity:[DateTimeArray count]];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd.MM.yyyy HH:mm:ss"];

    for(NSString *currentString in DateTimeArray){
        NSDate *date = [dateFormat dateFromString:currentString];
        [nsdateArray addObject:date];
    }
    [dateFormat release];
    NSLog(@"ArrayWithDate:%@",[nsdateArray description]);
水晶透心 2024-11-15 22:49:04

这是完整的代码集..希望它有所帮助..

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];
int i=0;//start for{} loop
NSString *currDate = [formatter dateFromString:[DateTimeArray objectAtIndex:i]];
[formatter release];

快乐 iCoding...

Heres the complete set of code.. Hope it helps..

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm:ss"];
int i=0;//start for{} loop
NSString *currDate = [formatter dateFromString:[DateTimeArray objectAtIndex:i]];
[formatter release];

Happy iCoding...

夏末 2024-11-15 22:49:04
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm:ss"]
NSString *theTime = [timeFormat dateFromString:date];
or
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(kCFCalendarUnitHour | kCFCalendarUnitMinute) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm:ss"]
NSString *theTime = [timeFormat dateFromString:date];
or
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(kCFCalendarUnitHour | kCFCalendarUnitMinute) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
听风吹 2024-11-15 22:49:03

代码如下,

for(int index=0;index<[DateTimeArray count];index++)
{
   NSDateFormatter *df = [[NSDateFormatter alloc] init];
   [df setDateFormat:@"HH:mm:ss"];
   NSString *timeString=[[[DateTimeArray objectAtIndex:index]componentsSeparatedByString:@" "]objectAtIndex:1];
   NSDate *time=[df dateFromString:timeString];
   [timeArray addObject:time];
   [df release];
}

Code as follows,

for(int index=0;index<[DateTimeArray count];index++)
{
   NSDateFormatter *df = [[NSDateFormatter alloc] init];
   [df setDateFormat:@"HH:mm:ss"];
   NSString *timeString=[[[DateTimeArray objectAtIndex:index]componentsSeparatedByString:@" "]objectAtIndex:1];
   NSDate *time=[df dateFromString:timeString];
   [timeArray addObject:time];
   [df release];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文