获取当前时间---年月日时分秒------iOS

方式一:XXXX年-XX月-XX日  XX时:XX分:XX秒的格式

- (IBAction)LoginAction:(UIButton *)sender

{

NSDate *date = [NSDate date];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    NSString *DateTime = [formatter stringFromDate:date];
    NSLog(@"%@============年-月-日  时:分:秒=====================",DateTime);

}

方式二:XXXX年-X月-X日  XX时:XX分:XX秒的格式  
- (IBAction)LoginAction:(UIButton *)sender{

NSDate *now = [NSDate date];
    NSLog(@"now date is: %@", now);
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    
    int year =(int) [dateComponent year];
    int month = (int) [dateComponent month];
    int day = (int) [dateComponent day];
    int hour = (int) [dateComponent hour];
    int minute = (int) [dateComponent minute];
    int second = (int) [dateComponent second];
    
    NSInteger year = [dateComponent year];
    NSInteger month =  [dateComponent month];
    NSInteger day = [dateComponent day];
    NSInteger hour =  [dateComponent hour];
    NSInteger minute =  [dateComponent minute];
    NSInteger second = [dateComponent second];

NSLog(@"year is: %ld", (long)year);
    NSLog(@"month is: %ld", (long)month);
    NSLog(@"day is: %ld", (long)day);
    NSLog(@" hour is: %ld",  (long)hour);
    NSLog(@"minute  is: %ld", (long)minute );
    NSLog(@"second is: %ld", (long)second);
     //字符串的转化并且拼接
      NSString *yearstr=[NSString stringWithFormat:@"%ld-",(long)year];
      NSString *monthstr=[NSString stringWithFormat:@"%ld-",(long)month];
     NSString *daystr=[NSString stringWithFormat:@"%ld ",(long)day];
     NSString *hourstr=[NSString stringWithFormat:@"%ld:",(long)hour];
    NSString *minutestr=[NSString stringWithFormat:@"%ld:",(long)minute];
    NSString *secondstr=[NSString stringWithFormat:@"%ld",(long)second];
    //字符串开始拼接
    NSString *allstr=[yearstr stringByAppendingString:monthstr];
    NSString *allstr1=[allstr stringByAppendingString:daystr];
    NSString *allstr2=[allstr1 stringByAppendingString:hourstr];
    NSString *allstr3=[allstr2 stringByAppendingString:minutestr];
    NSString *DateTime=[allstr3 stringByAppendingString:secondstr];
    NSLog(@"最后年月日时分秒拼接的结果=====%@",DateTime);
}

时间: 2024-08-07 17:24:05

获取当前时间---年月日时分秒------iOS的相关文章

sql server获取当前年月日 时分秒

获取当前年月日(字符串): select CONVERT(varchar(11),GETDATE(),112) 获取当前时间的时分秒(':'隔开): select CONVERT(varchar(12),GETDATE(),108) 将年月日时分秒拼接成一条字符串: select CONVERT(varchar(11),GETDATE(),112)+REPLACE(CONVERT(varchar(12),GETDATE(),108),':','')

Java计算年月日时分秒时间差(两个时间相减)

//测试主方法  public static void main(String[] args) {          Date currentTime = df.parse("2004-03-26 13:31:40");   //当前系统时间             Date firstTime = df.parse("2004-01-02 11:30:24");     //查询的数据时间          String str=getTime(currentTi

关于时间的操作(Java版)——将毫秒转换为年月日时分秒

第一种方式: import java.util.Calendar; import java.util.TimeZone; public class Test { /** * 将毫秒转换为年月日时分秒 * * @author GaoHuanjie */ public String getYearMonthDayHourMinuteSecond(long timeMillis) { Calendar calendar = Calendar.getInstance(TimeZone.getTimeZo

jquery 的日期时间控件(年月日时分秒)

<!-- import package --> <script type="text/javascript" src="JS/jquery.js"></script> <script type="text/javascript" src="JS/jquery-ui-1.7.3/ui/jquery-ui-1.7.3.custom.js"></script> <sc

获取年月日时分秒

NSDate *now = [NSDate date]; NSCalendar *cal = [NSCalendar currentCalendar]; unsigned int unitFlags = NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *dd = [cal components:unitFlags fromDate:

手机端日期时间控件(mobiscroll)设置年月日时分秒及时间格式

mobiscroll是个很好用的jquery日期插件,它可以实现在移动端滚动选择日期. 网上有很多相关的介绍:别人写过的我在这里不再重复,只记录自己的一些经验 首选,我用的是mobiscroll-2.13.2.full.min.js:至于其他版本没测试过,不敢保证使用效果完全相同. 进入主题,项目需要时间选择器提供年月日时分秒的格式. 网上搜索了一圈,只发现年月日时分的设置方法. 最后在官网找到解决办法 直接上代码 timeWheels: 'HHiiss',//HH:24小时制:hh:12小时制

C# 版本的 计时器类:精确到微秒 秒后保留一位小数 支持年月日时分秒带单位的输出

class TimeCount { // 临时变量,存放当前类能表示的最大年份值 private static ulong MaxYear = 0; /// <summary> /// 获取毫秒能表示的最大年份数 /// </summary> /// <returns>年份数最大值</returns> public static ulong GetMaxYearCount() { if (TimeCount.MaxYear != 0) return Time

计算一个人的年龄(年月日时分秒),有不对的地方希望大家指出!

想想我们可以做一个计时器,记录一下我们走过了多少时光.看了一下网上别人的一些代码,记录年月的都并不科学,甚至很麻烦,自己倒腾了一上午,总算弄出来了一个. 自己觉得还比较科学,暂时没有发现BUG,如果哪里有错,希望大家指出来! 上代码: <!doctype html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&

php实现显示网站运行时间-秒转换年月日时分秒

<?php // 设置时区 date_default_timezone_set('Asia/Shanghai'); /** * 秒转时间,格式 年 月 日 时 分 秒 * * @author [email protected] * @param int $time * @return array|boolean */ function Sec2Time($time){ if(is_numeric($time)){ $value = array( "years" => 0,