iOS - 获取系统时间年月日,阳历(公历)日期转农历的方法
//获取当前时间
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 - 获取系统时间年月日,阳历(公历)日期转农历的方法的更多相关文章
- Swift3.0 iOS获取当前时间 - 年月日时分秒星期
Swift3.0 iOS获取当前时间 - 年月日时分秒星期func getTimes() -> [Int] { var timers: [Int] = [] // 返回的数组 let calen ...
- ios获取系统时间
//获取系统时间 NSDate * date=[NSDate date]; NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; ...
- IOS获取系统时间 NSDate
//返回当前时间,精确到毫秒.- (NSString *)getTimeNow { NSString* date; NSDateFormatter * formatter = [[NSDateForm ...
- 【转】QT获取系统时间,以及设置日期格式
http://blog.csdn.net/zzk197/article/details/7498593 例如我要在一个label上设置当前时间 QDateTime time = QDateTime:: ...
- C语言 获取系统时间与睡眠时间函数
摘要: 以ms为单位,获取系统时间.睡眠或延迟时间函数的使用方法. #include<stdio.h> #include <time.h> #include <sys/t ...
- c++ 怎样获取系统时间
c++ 怎样获取系统时间 2008-04-28 15:34 //方案— 长处:仅使用C标准库:缺点:仅仅能精确到秒级 #include <time.h> #include <stdi ...
- VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法
1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...
- 【VS开发】VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法
1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...
- 按照相应的格式获取系统时间并将其转化为SQL中匹配的(date)时间格式
在获取时间时需要对时间格式进行设置,此时就需要用到SimpleDateFormat 类 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM ...
随机推荐
- 洛谷P3069 [USACO13JAN]牛的阵容Cow Lineup(尺取法)
思路 考虑比较朴素的解法,枚举每个长度为\(k+1\)的区间,然后统计区间中出现次数最多的颜色.这样的话复杂度为\(O(n*k)\)的,显然不行. 观察到统计每个区间中出现次数最多的颜色中,可以只用看 ...
- python 实现 DES CBC模式加解密
# -*- coding=utf-8-*- from Crypto.Cipher import DES import base64 """ des cbc加密算法 pad ...
- js 正则表达式2
对于某些特殊的字符,我们 必须转义一下才可以使用.(注意一点,我们使用那些需要转义的字符是通过"\"+相应的字符来构成的,记住是"\",而不也是"/& ...
- Cause: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect date value
Cause: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect date value: '154 ...
- ArcGIS 10.2 JavaScript API本地部署离线开发环境
1 获取ArcGIS JavaScript API API的下载地址http://support.esrichina.com.cn/2011/0223/960.html,在下载页面会看到api和sdk ...
- HDU6625: three arrays (字典树处理xor)
题意:给出A数组,B数组,你可以对A和B分别进行重排列,使得C[i]=A[i]^B[i]的字典序最小. 思路:对于这类题,显然需要建立字典树,然后某种形式取分治,或者贪心. 假设现在有了两颗字典树A ...
- 函数中,对形参做不加var的全局溢出赋值,可改变形参所指向的实参的本身值
var formateNumArr = function(arr,defaultVal){ var a = []; $.each(arr,function(i,v){ ...
- Aizu2249-Road Construction-(Dijkstra)
https://vjudge.net/problem/Aizu-2249 题意:计划图中有n个城市m条路,首都是1号城市,要选一些路去修,让各个城市到首都的路径最短,在路径最短的情况下修路费用最小. ...
- docker 清理无用的卷
docker system prune 对于卷的清理不够彻底 题外话:docker volume ls查看当前卷列表 使用如下命令可以清理不用的卷 docker volume prune -f 强制 ...
- 开源项目(9-0)综述--基于深度学习的目标跟踪sort与deep-sort
基于深度学习的目标跟踪sort与deep-sort https://github.com/Ewenwan/MVision/tree/master/3D_Object_Detection/Object_ ...