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 ...
随机推荐
- C++学习(4)——通讯录管理程序
复习简单操作,实现一个非常非常简单的通讯录管理小程序 #include <iostream> using namespace std; #include <string> co ...
- 洛谷 P1162 填涂颜色题解
题目描述 由数字00组成的方阵中,有一任意形状闭合圈,闭合圈由数字11构成,围圈时只走上下左右44个方向.现要求把闭合圈内的所有空间都填写成22.例如:6 \times 66×6的方阵(n=6n=6) ...
- Pytorch数据读取详解
原文:http://studyai.com/article/11efc2bf#%E9%87%87%E6%A0%B7%E5%99%A8%20Sampler%20&%20BatchSampler ...
- 某个新闻网站抓去自媒体账号 queryId js破解
第一步: 发现加密 第二部:搜索加密参数 queryId 第三部:找到js核心加密代码 第四部:代码实现 window = {} function utf8ToBase64(t) { console. ...
- Elasticsearch 概念理解
官方文档地址 Filebeat: https://www.elastic.co/cn/products/beats/filebeat https://www.elastic.co/guide/en/b ...
- hashCode、HashMap 的原理
hashCode 的契约是:如果两个对象相等,那么调用两个对象的 hashCode() 方法一定会返回相同的 hash 值. HashMap 中 存储桶 的原理: 当你在 hashMap 中存储值的时 ...
- wordpress调用指定分类文章如何实现
wordpress是很强大的cms系统,你可以通过相关函数就能实现相关的功能.很多网友会问wordpress怎么调用指定分类文章的呢?其实很简单,随ythah一起来看看吧,几行代码就解决了,代码如下 ...
- spark-shell操作hive
本文是在集群已经搭建好的基础上来说的,还没有搭建好集群的小伙伴还请自行百度! 启动spark-shell之前要先启动hive metastore 和 hiveservice2 hive --servi ...
- 如何保护你的 Python 代码 (一)—— 现有加密方案
https://zhuanlan.zhihu.com/p/54296517 0 前言 去年11月在PyCon China 2018 杭州站分享了 Python 源码加密,讲述了如何通过修改 Pytho ...
- First-class function
https://en.wikipedia.org/wiki/First-class_function In computer science, a programming language is sa ...