Jump out of NSDate

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

Feel any language about time format processing, time zone processing are many, recently been nsdate of various problems pit for a long

Let's take a look at NSDate's own questions.

1.NSDate

NSDate Get current time

NSDate *date=[nsdate Date];

[NSDate Date], the output is GMT time (GMT (Greenwich Mean time) represents GMT), and if you want to get the current time, you need to convert by timestamp

+ (NSDate *) Getlocaltimenow: (NSDate *) date{    *timezone=[Nstimezone systemtimezone];    Nsinteger interval=[timezone secondsfromgmtfordate:date];     *localdate=[Date datebyaddingtimeinterval:interval];     return localdate;}

The initial look is no problem, the first step to get the current time zone, the second step to calculate the difference between the current time zone and GMT time zone, the third step through the time zone difference to increase the value of the current time

Output Current time is

"2014-12-16 10:20:58 +0000"

Look at the back of the +0000 is not the feeling is wrong, if the current time zone is Beijing, then the time is wrong, it should be +0800, here I go around a circle, in fact, we use [NSDate Date] To obtain is the current time, but it is GMT time, If we add our time zone interval is only the GMT time added 8 hours, in fact, compared to US GMT time plus 8 hours output, converted to our local time will also increase 8 hours, below we use nsstring output try to see

Conversion of 2.NSDate to NSString

Apple uses NSDateFormatter to convert between NSDate and NSString, HH stands for 24-hour, and HH stands for 12-hour system

+ (NSString *) Makedatetostr: (NSDate *) date{        *datef=[[NSDateFormatter alloc]init];    [Datef Setdateformat: @" YYYY-MM-DD HH:mm:ss " ];     *datestr=[Datef stringfromdate:date];     return datestr;}

This time we enter [NSDate Date] to see if the output is the current time

Try it out and you can see

2014-12-16 10:20:58

The upper and lower code is run together, so it can be seen that if we use the default output of the formatted string is the local time, is not required to do time stamp conversion, if you need time to convert, just set the time zone is OK

Like what

Add a row of foreign time zones

@" America/adak "]];

Locatime:2014-12-16 11:31:38

Gmt:2014-12-16 03:31:38

America:2014-12-15 17:31:38


But what do I want to do with the output time zone, and if I change the format, I need to find out the format string description of Apple and find the Apple ISO document through the Help document.

Date Field Symbol Table

According to the zone format character description only need to add a large "Z", month and day hours and minutes of the time zone are separate formatting strings, so you can randomly arrange their order

That

+ (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); returndatestr;}

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

, Gmt:2014-12-16 05:41:17 +0000,

america:-1000 2014-12-15 19:41:17

So that we can convert the opposite direction.

+ (NSDate *) Getlocaltimenowfromstr: (NSString *) datestring{       *datef=[[NSDateFormatter alloc]init];    [Datef Setdateformat: @" yyyy-mm-dd HH:mm:ss Z " ];     *date=[Datef datefromstring:datestring];     return date;}
Input

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

Output

2014-12-16 05:41:17 +0000

Look, it's back to our GMT time zone.

To sum up, in fact, we do not need to do time zone conversion, all the places in GMT time zone as a time record, when you want to use time zone to turn good, local time zone conversion is very convenient

3.NSDate to. NET JSON format time

In the previous article, the time format for. NET format JSON is/date (xxxxxxxxxxxxx+xxxx)/, I tried not to pass through the afnetwork directly by the time, then can only format the string

The first step is to get the time and time interval of 1970, return the seconds to be replaced by milliseconds, that is, *1000, but also more two bits, using the formatted string "%0.0LF" to cancel the decimal point

The second step gets the time zone number to get the current time zone, returns the timestamp of the current time zone and the standard Time zone, the unit is seconds, needs x100 to offset the decimal point, because the need is 4 digits, needs 0 fill%04ld means does not meet 4 bits with 0 padding

The third step to get time, stitching strings, this everyone will be.

+ (NSString *) Makejsondate: (NSDate *) date{nstimeinterval interval=[date timeintervalsince1970]* +; NSString*datestr=[[nsstring Alloc]initwithformat:@"%0.0LF", Interval]; Nstimezone*zone=[Nstimezone Systemtimezone]; Nsinteger Zoneinterval=[Zone SECONDSFROMGMT]; Nsinteger Zonenum=zoneinterval/ -/ -* -; NSString*json=[[nsstring Alloc]initwithformat:@"/date (%@+%04LD)/", Datestr,zonenum]; returnJSON;}

At this point, we finally solved the headache.

In the future, use strings to pass the time ....

Jump out of NSDate

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.