将日历日期转换为正确的日期

时间:2017-05-04 07:47:26

标签: ios objective-c xcode calendar nsdate

我正在开发一个使用日历创建警报的应用程序。我可以在正确的日期正确设置警报。例如,我为// Get context with jQuery - using jQuery's .get() method. var areaChartCanvas = $("#areaChart").get(0).getContext("2d"); // This will get the first returned node in the jQuery collection. var areaChart = new Chart(areaChartCanvas); var areaChartData = { labels: ["January", "February", "March", "April", "May", "June", "July","August", "September", "October", "November", "December"], datasets: [ { label: "Electronics", fillColor: "rgba(210, 214, 222, 1)", strokeColor: "rgba(210, 214, 222, 1)", pointColor: "rgba(210, 214, 222, 1)", pointStrokeColor: "#c1c7d1", pointHighlightFill: "#fff", pointHighlightStroke: "rgba(220,220,220,1)", data: [65, 59, 80, 81, 56, 55, 40] } ] }; 设置了闹钟。

当我尝试获取日历事件时,它会返回UTC中的其他日期。

enter image description here

如您所见,它在同一天返回UTC 4th of May 2017 1 PM。我想知道如何在我尝试从日历中获取确切日期时获得确切的日期。

3 个答案:

答案 0 :(得分:1)

您只需将 UTC 转换为您当地的时区

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSDate *date1 = [dateFormatter dateFromString:@"2017-05-04 10:00:00"];

// change to a readable time format and change to local time zone
[dateFormatter setDateFormat:@"MM/dd/yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *strCurrentLocalTimezoneDate = [dateFormatter stringFromDate:date1];

答案 1 :(得分:0)

Date总是占用当前时区,直到我们更改其他时间。如果我们打印Date它可能会显示不同但实际上它需要当前。

答案 2 :(得分:-1)

//除了这段代码,您可能还需要设置timeZone。

NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"MMM-dd-yyyy"];

NSDate *now = [[NSDate alloc] init];

NSString *dateString = [format stringFromDate:now];
    NSLog(@"%@",dateString);