从 UIPickerView 获取月份

发布于 2024-10-13 12:32:16 字数 772 浏览 10 评论 0原文

我有一个独特的要求。在我的应用程序中,我只需要从选择器中获取月份。由于日期选择器无法实现此功能,因此我只列出了几个月的自定义选择器。客户选择月份后所需的日期格式是 1-Jan-2011 到 1-feb-2011

基本上,月份是来自 uipickerview 的唯一内容,而日期和年份是静态的。 有人可以帮我吗?我尝试过一些代码。

    NSString *dateString = [[NSString alloc ]initWithFormat:@"2011-%d-1 GMT+05:30", monthFromPickerAsInteger];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        // this is imporant - we set our input date format to match our input string
        // if format doesn't match you'll get nil from your string, so be careful
    [dateFormatter setDateFormat:@"yy-MM-DD z4"];
    NSDate *dateFromString = [[NSDate alloc] init];
        // ta-daaa!
    dateFromString = [dateFormatter dateFromString:dateString];
    NSLog(@"%@", dateFromString);

I have a unique requirement. In my app I need to get only the month from picker. Since Date picker cannot implement this, I made the custom picker listing only months. The required format of the date after the customer selects a month is 1-Jan-2011 to 1-feb-2011

Basically, the month is the only thing that is coming from uipickerview and the day and year are static.
Can someone help me out? Some code I have tried.

    NSString *dateString = [[NSString alloc ]initWithFormat:@"2011-%d-1 GMT+05:30", monthFromPickerAsInteger];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        // this is imporant - we set our input date format to match our input string
        // if format doesn't match you'll get nil from your string, so be careful
    [dateFormatter setDateFormat:@"yy-MM-DD z4"];
    NSDate *dateFromString = [[NSDate alloc] init];
        // ta-daaa!
    dateFromString = [dateFormatter dateFromString:dateString];
    NSLog(@"%@", dateFromString);

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

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

发布评论

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

评论(1

梦幻的心爱 2024-10-20 12:32:16

我猜你想这样做

NSString *dateString = [NSString stringWithFormat:@"2011-%d-1", monthFromPickerAsInteger];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-M-d"]; 
NSDate *dateFromString = [dateFormatter dateFromString:dateString];

[dateFormatter setDateFormat:@"d-MMM-yyyy"];
NSString *requiredDateString = [dateFormatter stringFromDate:dateFromString];
[dateFormatter release];    

NSLog(@"%@", requiredDateString);

I guess you want to do this,

NSString *dateString = [NSString stringWithFormat:@"2011-%d-1", monthFromPickerAsInteger];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-M-d"]; 
NSDate *dateFromString = [dateFormatter dateFromString:dateString];

[dateFormatter setDateFormat:@"d-MMM-yyyy"];
NSString *requiredDateString = [dateFormatter stringFromDate:dateFromString];
[dateFormatter release];    

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