NSDate is stored in world standard Time (UTC) and needs to be converted to local time when output
It seems to be starting from ios4.1 [NSDate Date]; It's GMT time, which is 8 hours away from Beijing time, and the following code solves this problem.
Method One
-(void) tdate
{
NSDate *date = [Nsdatedate];
Nstimezone *zone = [Nstimezonesystemtimezone];
Nsinteger interval = [zone secondsfromgmtfordate:date];
NSDate *localedate = [date Datebyaddingtimeinterval:interval];
NSLog (@ "%@", localedate);
}
Method Two
-(nsstring*) dateasstring: (nsdate*) Date
{
/* Create A single string expressing a mountain ' s climbed date, properly localized * *
NSString *returnvalue = @ "";
NSDateFormatter *dateformatter = nil;
if (date!= nil) {
if (Dateformatter ==nil) {
Dateformatter = [[Nsdateformatteralloc]init];
}
Original address: http://blog.csdn.net/diyagoanyhacker/article/details/7096612
Author: to Strong
[Dateformatter Setdatestyle:nsdateformattermediumstyle];
[Dateformatter Settimestyle:nsdateformatternostyle];
[Dateformatter Setlocale:[nslocalecurrentlocale]];
returnvalue = [Dateformatter stringfromdate:date];
}
/* As this code uses the current "locale", the date format is in the format
Specified by the user S "Region Format" settings. If you are need to
Alternate format internally, can create and use nslocales, e.g.:
Nslocale *engblocale = [[Nslocale alloc] initwithlocaleidentifier:@ "EN_GB"];
[Numberformatter Setlocale:engblocale];
Doing so won't affect the current User-set language or region format.
Similarly, while your should always rely on the system and application bundle
To pick the most appropriate, the "Current user language setting,
If you are need to know what the current user language setting are, you can do
Something like the following:
nsuserdefaults* defs = [Nsuserdefaults standarduserdefaults];
nsarray* languages = [defs objectforkey:@ "Applelanguages"];
nsstring* Preferredlang = [languages objectatindex:0];
NSLog (@ "Current language is%@", Preferredlang);
The IPhone does not support locales in the same way that Mac OS
Does (really only using locales for the Region Format settings)
Try and get a array from Standarduserdefaults for the key "Applelocale",
This is fail on the IPhone. */
Return returnvalue;
}
Method Three
+ (NSString *) Fixstringfordate: (NSDate *) Date
{
nsdateformatter* dateformatter = [[Nsdateformatteralloc]init];
[Dateformatter Setdatestyle:kcfdateformatterfullstyle];
[Dateformatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];
NSString *fixstring = [Dateformatter stringfromdate:date];
[Dateformatter release];
return fixstring;
}
Method Four
Nsdatecomponents *comps = [[Nsdatecomponentsalloc]init];
[Comps setyear:2010];
[Comps Setmonth:8];
[Comps setday:24];
[Comps sethour:17];
[Comps Setminute:5];
[Comps Settimezone: [nstimezonetimezonewithabbreviation:@ ' UTC ']];
NSLog (@ "%@", [nstimezonetimezonewithabbreviation:@ "UTC"]);
Nscalendar *cal = [[Nscalendaralloc]initwithcalendaridentifier:nsgregoriancalendar];
NSDate *referencetime = [cal Datefromcomponents:comps];
NSLog (@ "%@", referencetime);
The following information is reproduced
time concepts such as GMT, UTC, and 24 time zones
Many people know that the two schedules are either GMT or UTC, and that the world time table is known as the Global Times, so what is the actual intent of GMT and UTC. How the world time zone is differentiated. The dense English words on the plate represent what meaning and function. These are the novice in contact with the timetable or the World time Zone table, the mind of the various questions, the following will take you to explore the time zone mystery.
Map of world time zone and International Day-changing line
Division of 24 time zones worldwide
Compared to the two-time timetable, it is possible to show the world timing table of times and place names in the world's time zones, which is more sophisticated and complex, and usually the dial of the world's time zone table will mark the names of the 24 time zones around the globe, but how exactly are the 24 time zones produced In the past, local time has been set for each place, but with the development of transport and telecommunications, the increasing frequency of exchanges, the different local time, causing many problems, so at the International conference in 1884 when the development of global standards, Designated in Greenwich, England, this place is the beginning of the zero longitude (also known as Meridian), and the Earth from west to east every 24 hours a week , set the longitude 15°, time difference 1 hours. The longitude of each 15 ° is called the Central warp of the time zone, dividing the world into 24 time zones, containing 23 whole time zones and 2 and a half time zones on both sides of the 180° warp. In terms of global time, the time of east longitude is earlier than the West longitude, that is, if GMT is 12 o'clock noon, then the central warp 15°e time zone is 1 o'clock in the afternoon, the central warp 30°e timezone is 2 o'clock in the afternoon; conversely, the Central warp 15°w time zone is 11 o'clock in the morning, and the central Warp is 30°. The time of the W time zone is 10 o'clock in the morning. Taiwan, for example, is located at 121 °c and has a 8-hour jet lag after conversion to Greenwich. If they were both heading east from 0° in Greenwich, the west advances, when they are in the warp 180°, it will be 24 hours, so the warp 180° is designated as the International Day Line, from west to east through this line date to subtract day, on the contrary, if from east to West will be added to the days.
Greenwich Standard Time GMT
In 17th century, the Greenwich Royal Observatory carried out celestial observations for the purpose of expanding its maritime hegemony. The 1675 Old Royal Observatory (Observatory) was formally established, and in 1884 it was decided that the meridian of Greenwich would be used to divide the 0 degrees of longitude between the two hemispheres of the Earth. There is a sign on the wall of the observatory's 24-hour clock, showing the time of the moment, for the world, the time set here is the world time reference point, and the world is setting time as a standard in Greenwich time, which is our familiar "Greenwich Standard Time" (Greenwich Mean Times , referred to as g.m.t.) The origin, marked on the watch, then represents the table has the time function of two places, that is, also can show the origin and the time of another country.
World Coordinated time UTC
Most of the two schedules are expressed as GMT, but there are also some two schedules where GMT is not seen, instead of the 3 English letters UTC, what is UTC. In fact, UTC refers to the Coordinated universal time-World coordinated Time (also known as World standard Time, world unification Time), which passes through the mean solar time (whichever is GMT), the revised Earth axis movement, and the " Seconds "is the time when the international atom of the Unit is consolidated, the calculation process is quite precise, so UTC is more accurate than GMT in terms of" world standard Time ". The error value must remain within 0.9 seconds, and if greater than 0.9 seconds is released by the International Earth Rotation transaction NCBs in Paris, the UTC will be the same as the Earth rotation cycle. So essentially the nature of UTC emphasizes a more precise world time standard than GMT, but for the current table, the functionality and accuracy of GMT and UTC are no different.
Summer saves time DST
The so-called "summer savings"daylight Saving Time "(referred to as D.S.T), refers to the summer sun rises earlier, the clock to speed up an hour to use the early daylight, in the United Kingdom is called the Daylight Time between (Summer time). The idea was proposed by the US Benjamin Franklin in 1784, and in 1915 Germany became the first country to formally implement daylight saving time in daylight savings to cut lighting and power consumption. Since then, the practice has been cited by about 70 countries around the world, mainly in Europe and North America. India, which is now divided into two time zones, is also discussing whether daylight saving time in daylight savings should be uniformly implemented across the country. European mobile phone also has a lot of GSM system base station, in addition to the local time will also include daylight saving time, as a mobile phone time standard, users can decide to turn on or off. It is noteworthy that some countries have a "summer save Time" system, do not forget to go abroad to follow the local habit of adjusting on the table, this is the mechanical table does not have the function of design oh.