将 NSDate 从日历转换为另一个 [英] Converting NSDate from a calendar to another

查看:61
本文介绍了将 NSDate 从日历转换为另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遇到了麻烦.花了我几个小时,但没有用.请记住,我检查了 StackOverFlow 的解决方法,但没有.

I'm having trouble here. Took me hours but no use. Please keep in mind I checked the heck of StackOverFlow for a fix but there isn't any.

当我将 _date 对象从 Gregorian 转换为 Hijri 时,它不会转换.我知道这是因为当我打印它时,它仍然给了我 Gregorian 日.这让我很烦恼,因为我想要 UIDatePickersetDate 并且它设置不正确,因为 _dategregorian 和我的 UIDatePicker 对象日历设置为 setCalendar:_hijriCalendar

When I convert the _date Object from Gregorian to Hijri it does not convert. I know that because when I print it it still gives me the Gregorian day. This troubles me because I want to setDate of a UIDatePicker and it's setting it incorrectly because the _date is gregorian and my UIDatePicker object calendar set to setCalendar:_hijriCalendar

//DATE SETTING
_date = [NSDate date];
_hijriCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSIslamicCalendar];
_gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

unsigned units = NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit;

NSDateComponents *currentDateComponents = [_hijriCalendar components:units fromDate:[NSDate date]];


NSDate *islamicDate =[_hijriCalendar dateFromComponents:currentDateComponents];

NSLog(@" Current Date: %@",islamicDate);  //this outputs gregorian current date not Hijri

注意:伙计们,我不想只打印日期.我知道使用设置为正确的 NSCalendar 对象的 NSDateFotmatter 可以完成这项工作.但我想将日期转换为新日期以将其设置为我的 UIDatePicker.我希望你们明白我的意思.

NOTE: Guys, I don't want just print the date. I know using a NSDateFotmatter set to the correct NSCalendar object will do the job. But I want to convert the date to a new date to set it to my UIDatePicker. I hope you guys get what I mean.

推荐答案

NSDate 对象代表一个时刻, 与任何日历、时区或相对论无关(这最后一个通常被认为不是地球计算机科学中的问题.)

An NSDate object represents a moment in time, irrespective of any calendar, time zone, or relativity (this last one is generally considered to be a non-issue in Earth-based computer science.)

当您通过任何方式创建 NSDate 的实例时,它会被简单地存储为自固定时间点(格林威治标准时间 2001 年 1 月 1 日凌晨 12:00)以来的秒数iOS 和 OS X.)

When you create an instance of NSDate through any means, it is stored as simply the number of seconds since a fixed point in time (12:00 AM GMT on January 1, 2001 in the case of iOS and OS X.)

因此,将日期从公历转换为回历不会任何事情,因为固定时刻保持不变.

As a result, converting a date from Gregorian to Hijri doesn't do anything because that fixed moment remains the same.

如果您具体告诉我们您希望在公历和回历之间发生哪些尚未发生的转变,我们可以建议替代行动方案.

If you tell us specifically what transformation you expect to occur between Gregorian and Hijri that is not already occurring, we can suggest an alternate course of action.

这篇关于将 NSDate 从日历转换为另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆