Jump out of NSDate, nsdate

Source: Internet
Author: User
Tags gmt time zone time zones

Jump out of NSDate, nsdate

I feel that there are many time zones for formatting in any language, and it has been plagued by various NSDate problems for a long time.

Let's take a look at the NSDate issue.

1. NSDate

NSDate get current time

NSDate *date=[NSDate date];

[NSDate date], the output is the GMT Time (GMT (Greenwich Mean Time) represents the Greenwich Mean Time). If you want to obtain the current Time, you need to use the timestamp for conversion.

+(NSDate *)GetLocalTimeNow:(NSDate *)date{    NSTimeZone *timezone=[NSTimeZone systemTimeZone];    NSInteger interval=[timezone secondsFromGMTForDate:date];    NSDate *localdate=[date dateByAddingTimeInterval:interval];    return localdate;}

Initially it seems that there is no problem. The first step is to get the current time zone, the second step is to calculate the difference between the current time zone and the GMT time zone, and the third step is to increase the difference through the time zone difference to get the current time zone

The current output time is

"10:20:58 + 0000"

Check if the + 0000 is incorrect. If the current time zone is beijing, the time is incorrect. It should be + 0800. Here I went around a circle, in fact, what we get with [NSDate date] is the current time, which is only the GMT time. If we add our own time zone, we only add the GMT time to 8 hours, in fact, The GMT time plus 8 hours of output will also increase by 8 hours if it is converted to our local time. Let's use NSString output for a try.

2. NSDate and NSString Conversion

Apple uses NSDateFormatter to convert between NSDate and NSString. HH indicates the 24-hour system and hh indicates the 12-hour system.

+(NSString *)MakedatetoStr:(NSDate *)date{        NSDateFormatter *datef=[[NSDateFormatter alloc]init];    [datef setDateFormat:@"yyyy-MM-dd HH:mm:ss"];    NSString *datestr=[datef stringFromDate:date];    return datestr;}

This time we enter the [NSDate date] Time to see if the output is the current time.

Try it. You can see

10:20:58

The upper and lower codes run together, so we can see that if the default output of formatted strings is local time, no timestamp conversion is required. If time conversion is required, you only need to set the time zone.

For example

Add a foreign Time Zone

[datef setTimeZone:[NSTimeZone timeZoneWithName: @"America/Adak"]];

Locatime: 11:31:38

GMT: 03:31:38

America: 17:31:38


But what should I do if I want to output the time zone? What should I do if I change the format? I need to find the formatting string instruction document of apple. I found the ISO document of apple through the help document.

Date Field Symbol Table

According to the zone formatting characters, you only need to add a large "Z". year, month, day, hour, minute, and second time zones are separate formatted strings, so you can arrange their order as needed.

That is

+(NSString *)MakedatetoStr:(NSDate *)date{        NSDateFormatter *datef=[[NSDateFormatter alloc]init];     NSDateFormatter *datef2=[[NSDateFormatter alloc]init];    [datef2 setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];    [datef setDateFormat:@"Z yyyy-MM-dd HH:mm:ss}"];    [datef setTimeZone:[NSTimeZone timeZoneWithName: @"America/Adak"]];    NSString *datestr=[datef stringFromDate:date];    NSLog(@"locatime%@,GMT:%@,America:%@",[datef2 stringFromDate:date],date,datestr);    return datestr;}

 

Locatime2014-12-16 13:41:17 + 0800

, GMT: 05:41:17 + 0000,

America:-1000 19:41:17

 

In this way, we can switch the opposite direction.

+(NSDate *)GetLocalTimeNowFromstr:(NSString *)dateString{       NSDateFormatter *datef=[[NSDateFormatter alloc]init];    [datef setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];    NSDate *date=[datef dateFromString:dateString];    return date;}
Input

13:41:17 + 0800

Output

05:41:17 + 0000

Look back to our GMT time zone.

To sum up, we don't need to convert the time zone when we get the time. All the places use the GMT time zone as the time record. If you want to use the time zone, you just need to change it, local time zone conversion is very convenient

3. NSDate to. net Json format time

As described in the previous article, the time format after. net formatting Json is/Date (xxxxxxxxxxxxxxx + xxxx)/. I tried to format strings instead of passing in strings by time through AFNetwork.

The first step is to obtain the time interval between 1970 and 1000. The return time must be changed to milliseconds, that is, *, but two more digits are returned. The "% 0.0lf" formatted string is used to cancel the decimal point.

The second step gets the time zone number to get the current time zone, and returns the timestamp of the current time zone and the Standard Time Zone. The unit is second, And the decimal point needs to be offset by X 100 because it requires four digits, % 04ld needs to be filled with zero, indicating that four digits are not filled with zero

Step 3: Obtain the time and splice the string ..

+(NSString *)MakeJsonDate:(NSDate *)date{    NSTimeInterval interval=[date timeIntervalSince1970]*1000;    NSString *datestr=[[NSString alloc]initWithFormat:@"%0.0lf",interval];    NSTimeZone *zone=[NSTimeZone systemTimeZone];    NSInteger zoneinterval=[zone secondsFromGMT];    NSInteger zonenum=zoneinterval/60/60*100;    NSString *json=[[NSString alloc]initWithFormat:@"/Date(%@+%04ld)/",datestr,zonenum];    return json;}

So far, this headache has finally been solved.

In the future, use a string to pass the time ....

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.