简体   繁体   English

当前日期和时间 - NSDate

[英]current Date and Time - NSDate

I need to display the current Date and Time. 我需要显示当前的日期和时间。

I have used ; 我用过 ;

NSDate *currentDateNTime        = [NSDate date];

I want to have the current date and time (Should display the system time and not GMT time). 我想拥有当前的日期和时间(应该显示系统时间而不是GMT时间)。 The output should be in a NSDate format and not NSString . 输出应该是NSDate格式而不是NSString

for example; 例如;

    NSDate *currentDateNTime        = [NSDate date];
// Do the processing....

NSDate *nowDateAndTime = .....; // Output should be a NSDate and not a NSString

Since all NSDate is GMT referred, you probably want this: (don'f forget that the nowDate won't be the actual current system date-time, but it's "shifted", so if you will generate NSString using NSDateFormatter, you will see a wrong date) 由于所有的NSDate都是GMT引用的,你可能想要这样:(不要忘记nowDate不是实际的当前系统日期时间,而是“移位”,所以如果你使用NSDateFormatter生成NSString,你会看到错误的日期)

NSDate* currentDate = [NSDate date];
NSTimeZone* currentTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* nowTimeZone = [NSTimeZone systemTimeZone];

NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:currentDate];
NSInteger nowGMTOffset = [nowTimeZone secondsFromGMTForDate:currentDate];

NSTimeInterval interval = nowGMTOffset - currentGMTOffset;
NSDate* nowDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:currentDate];

Every moment in time is the same moment in time everywhere around the world —- it is just expressed as different clock times in different timezones. 每个时刻都是世界各地的同一时刻 - 它只是表示为不同时区的不同时钟时间。 Therefore, you can't change the date to some other date that represents the time in your timezone; 因此,您无法将日期更改为表示时区中某个时间的其他日期; you must use an NSDateFormatter that you feed with the timezone you are in. The resulting string is the moment in time expressed in the clock time of your position. 您必须使用您所在的时区提供的NSDateFormatter 。结果字符串是您的位置时钟时间表示的时刻。

Do all needed calculations in GMT, and just use a formatter for displaying. 在GMT中完成所有需要的计算,只需使用格式化程序进行显示。

Worth reading Does [NSDate date] return the local date and time? 值得一读[NSDate date]是否返回当地日期和时间?

Some useful resources for anyone coming to this more recently: 对于最近来到这里的人来说,有一些有用的资源:

Apple date and time programming guide do read it if you're doing anything serious with dates and times. Apple日期和时间编程指南如果您正在做任何严肃的日期和时间,请阅读它。 https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html#//apple_ref/doc/uid/10000039i?language=objc https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html#//apple_ref/doc/uid/10000039i?language=objc

Useful category on NSDate with lots of utilities does allow a ~new~ date to be generated based on an existing date. 具有大量实用程序的NSDate上的有用类别允许基于现有日期生成~new_date。 https://github.com/erica/NSDate-Extensions https://github.com/erica/NSDate-Extensions

There's also a swift version of the category https://github.com/erica/SwiftDates 还有一个类别https://github.com/erica/SwiftDates的快速版本

You need an NSDateFormatter and call stringFromDate this method to get a string of your date. 你需要一个NSDateFormatter并调用stringFromDate这个方法来获取你的日期字符串。

NSDateFormatter *dateformater = [[NSDateFormatter alloc] init];
[dateformater setDateFormat:@"yyyyMMdd,HH:mm"];
NSString *str = [dateformater stringFromDate: currentDateNTime];

use this method 使用这种方法

-(NSDate *)convertDateToDate:(NSDate *) date
{
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
    NSDate *nowDate = [[[NSDate alloc] init] autorelease];
    [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [formatter setDateFormat:@"yyyy-MM-d H:m:s"];
    NSString * strdate = [formatter stringFromDate:date];
    nowDate = [formatter dateFromString:strdate];
    return nowDate;
}

this may return you what you want. 这可能会回报你想要的东西。

i hope you this may help you. 我希望你能帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM