//获取当前时间
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 = [dateComponent year];
int month = [dateComponent month];
int day = [dateComponent day];
int hour = [dateComponent hour];
int minute = [dateComponent minute];
int second = [dateComponent second]; NSLog(@" year is: %d ",year);
NSLog(@" month is: %d ",month);
NSLog(@" day is: %d ",day);
NSLog(@" hour is: %d ",hour);
NSLog(@" minute is: %d ",minute);
NSLog(@" second is: %d ",second);
NSString *dateTime = [self LunarForSolarYear:year Month:month Day:day];

//农历转换函数
- (NSString *)LunarForSolarYear:(int)wCurYear Month:(int)wCurMonth Day:(NSInteger)wCurDay {
//农历日期名
NSArray *cDayName = [NSArray arrayWithObjects:@"*",@"初一",@"初二",@"初三",@"初四",@"初五",@"初六",@"初七",@"初八",@"初九",@"初十", @"十一",@"十二",@"十三",@"十四",@"十五",@"十六",@"十七",@"十八",@"十九",@"二十", @"廿一",@"廿二",@"廿三",@"廿四",@"廿五",@"廿六",@"廿七",@"廿八",@"廿九",@"三十",nil];
//农历月份名
NSArray *cMonName = [NSArray arrayWithObjects:@"*",@"正",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"十",@"十一",@"腊",nil];
//公历每月前面的天数
const int wMonthAdd[] = {,,,,,,,,,,,};
//农历数据
const int wNongliData[] = {,,,,,,,,, ,,,,,,,,,, ,,,,,,,,,, ,,,,,,,,,, ,,,,,,,,,, ,,,,,,,,,, ,,,,,,,,,, ,,,,,,,,,, ,,,,,,,,,, ,,,,,,,,,,}; static int nTheDate,nIsEnd,m,k,n,i,nBit;
//计算到初始时间1921年2月8日的天数:1921-2-8(正月初一)
nTheDate = (wCurYear - ) * + (wCurYear - ) / + wCurDay + wMonthAdd[wCurMonth - ] - ; if((!(wCurYear % )) && (wCurMonth > ))
nTheDate = nTheDate + ; //计算农历天干、地支、月、日
nIsEnd = ;
m = ; while(nIsEnd != ) {
if(wNongliData[m] < )
k = ;
else
k = ;
n = k;
while(n>=) {
//获取wNongliData(m)的第n个二进制位的值
nBit = wNongliData[m];
for(i=;i< n+;i++)
nBit = nBit/;
nBit = nBit % ;
if (nTheDate <= ( + nBit)) {
nIsEnd = ;
break;
}
nTheDate = nTheDate - - nBit;
n = n - ;
}
if(nIsEnd)
break;
m = m + ;
} wCurYear = + m;
wCurMonth = k - n + ;
wCurDay = nTheDate; if (k == ) {
if (wCurMonth == wNongliData[m] / + )
wCurMonth = - wCurMonth;
else if (wCurMonth > wNongliData[m] / + )
wCurMonth = wCurMonth - ;
}
//生成农历月
NSString *szNongliMonth;
if (wCurMonth < ){
szNongliMonth = [NSString stringWithFormat:@"闰%@",(NSString *)[cMonName objectAtIndex:- * wCurMonth]];
}else{
szNongliMonth = (NSString *)[cMonName objectAtIndex:wCurMonth];
}
//生成农历日
NSString *szNongliDay = [cDayName objectAtIndex:wCurDay];
//合并
NSString *lunarDate = [NSString stringWithFormat:@"农历%@月%@",szNongliMonth,szNongliDay];
return lunarDate;
}

iOS - 获取系统时间年月日,阳历(公历)日期转农历的方法的更多相关文章

  1. Swift3.0 iOS获取当前时间 - 年月日时分秒星期

    Swift3.0 iOS获取当前时间 - 年月日时分秒星期func getTimes() -> [Int] { var timers: [Int] = [] // 返回的数组 let calen ...

  2. ios获取系统时间

    //获取系统时间 NSDate * date=[NSDate date]; NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; ...

  3. IOS获取系统时间 NSDate

    //返回当前时间,精确到毫秒.- (NSString *)getTimeNow { NSString* date; NSDateFormatter * formatter = [[NSDateForm ...

  4. 【转】QT获取系统时间,以及设置日期格式

    http://blog.csdn.net/zzk197/article/details/7498593 例如我要在一个label上设置当前时间 QDateTime time = QDateTime:: ...

  5. C语言 获取系统时间与睡眠时间函数

    摘要: 以ms为单位,获取系统时间.睡眠或延迟时间函数的使用方法. #include<stdio.h> #include <time.h> #include <sys/t ...

  6. c++ 怎样获取系统时间

    c++ 怎样获取系统时间 2008-04-28 15:34 //方案— 长处:仅使用C标准库:缺点:仅仅能精确到秒级 #include <time.h> #include <stdi ...

  7. VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法

    1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...

  8. 【VS开发】VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法

    1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...

  9. 按照相应的格式获取系统时间并将其转化为SQL中匹配的(date)时间格式

    在获取时间时需要对时间格式进行设置,此时就需要用到SimpleDateFormat 类 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM ...

随机推荐

  1. 纯数据结构Java实现(10/11)(2-3树&红黑树)

    欢迎访问我的自建博客: CH-YK Blog.

  2. MyBatis-plus的入门学习

    MyBatis优势: Sql简单语句可以自由控制,更灵活,性能更高.. sql与代码分离,易于阅读和维护 提供xml标签,支持编写动态sql语句. 劣势: 简单crud操作还是写SQL 语句 xml中 ...

  3. Spring Boot 中的测试:JUnit

    官方文档:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html

  4. CF300D Painting Square

    Painting Square https://codeforces.com/problemset/problem/300/D 给了一个理解起来较复杂但是本质上很简单的分形. 题解 很显然,只有边长为 ...

  5. shell 编程生成日期文件;Server虚拟机上进行Web服务器配置

    shell 编程生成日期文件 1. 请编写一个脚本,命名为sh01.sh,其功能是: 键盘输入文件名(要求使用名字全拼作为文件名). 自动创建3个文件. 1个为系统当天日期(CCYYMMDD). 1个 ...

  6. signed Unsigned Compare

    // signUnsignCompare.cpp : Defines the entry point for the console application. // #include "st ...

  7. spark调优——Shuffle调优

    在Spark任务运行过程中,如果shuffle的map端处理的数据量比较大,但是map端缓冲的大小是固定的,可能会出现map端缓冲数据频繁spill溢写到磁盘文件中的情况,使得性能非常低下,通过调节m ...

  8. IDEA创建Mybatis的配置文件---sqlMapConfig.xml

    Mybatis的配置文件不像Spring的配置文件,在Maven当中添加过依赖之后就可以在下面这个地方打开,需要自己去手动去编写配置文件,但是自己编写的话会记不住要引入的DTD,所以就需要自己创建一个 ...

  9. RabbitMQ六种队列模式-主题模式

    前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主题模式 [ ...

  10. [Algorithm] 387. First Unique Character in a String

    Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...