(Turn) [IOS]ios how to get the time hour

 

 

Reprinted from: http://www.cnblogs.com/zhangdadi/archive/2012/07/24/2606305.html

 

method one:

NSDate *date = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *hourComponents = [cal components:NSCalendarUnitHour fromDate:_datePicker.date];
NSDateComponents *minuteComponents = [cal components:NSCalendarUnitMinute fromDate:_datePicker.date];

NSString *strHour = [NSString stringWithFormat:@"hour is %d", [hourComponents hour]];
NSString *strMin = [NSString stringWithFormat:@"minute is %d", [hourComponents minute]];

  

Method 2: (not work)

NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"HH"];
NSString *strHour = [dateFormatter stringFromDate:date];

 

  

NSDateFormatter code to adjust time format

Sometimes you need to adjust the time format to the format you want. At this time, we can use the NSDateFormatter class to handle it.
For example:
//Instantiate an NSDateFormatter object
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

//Set the time format, here you can set the format you need
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm :ss"];

//Use [NSDate date] to get the current system time
NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];

//The output format is: 2010-10-27 10:22:13
NSLog(@ "%@",currentDateStr);

//Don't forget to release the unused objects after alloc
[dateFormatter release];

 

 

NSDateComponents

NSDateComponents encapsulates date components in an extensible, object-oriented manner. It is used to make up time with the date and time components provided by a specified date: hour, minute, second, day, month, year, etc. It can also be used to specify a time, for example, 5 hours and 16 minutes. An NSDateComponents object does not need to define all component fields. When a new instance of NSDateComponents is created, the date component is set to NSUndefinedDateComponent.

 

An NSDateComponents object is meaningless by itself; you need to know what calendar interpretation it is for, you need to know if its value is a positive integer and what the value is.

 

An instance of NSDateComponents is not responsible for answering information other than a date, it needs to be initialized first. For example, if you initialize an object for May 6, 2004, its day of the week is NSUndefinedDateComponent, not Thursday. To get the correct day of the week, you have to create an NSCalendar calendar instance, create an NSDate object and use the dateFromComponents: method, then use components:fromDate: to retrieve the weekday

 

Getting Information About an NSDateComponents Object

Get information about an NSDateComponents object

  • – era era
  • – year year
  • – month month
  • – day
  • – hour 时
  • – Minute minutes
  • – second seconds
  • – week
  • – weekday
  • – weekdayOrdinal
  • – quarter quarter

 

Setting Information for an NSDateComponents Object

Set the information of an NSDateComponents object

  • – setEra:
  • – setYear:
  • – setMonth:
  • – setDay:
  • - setHour:
  • – setMinute:
  • – setSecond:
  • – setWeek:
  • – setWeekday:
  • – setWeekdayOrdinal:
  • – setQuarter:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326291424&siteId=291194637