显示今天的日期js代码(阳历和农历)


Posted in Javascript onSeptember 30, 2014

1.显示日期代码:

Js代码

/*获取当前日期*/ 
function getCurrentDateTime() { 
var d = new Date(); 
var year = d.getFullYear(); 
var month = d.getMonth() + 1; 
var date = d.getDate(); 
var week = d.getDay(); 
/*时分秒*/ 
/*var hours = d.getHours(); 
var minutes = d.getMinutes(); 
var seconds = d.getSeconds(); 
var ms = d.getMilliseconds();*/ 
var curDateTime = year; 
if (month > 9) 
curDateTime = curDateTime + "年" + month; 
else 
curDateTime = curDateTime + "年0" + month; 
if (date > 9) 
curDateTime = curDateTime + "月" + date + "日"; 
else 
curDateTime = curDateTime + "月0" + date + "日"; 
/*if (hours > 9) 
curDateTime = curDateTime + " " + hours; 
else 
curDateTime = curDateTime + " 0" + hours; 
if (minutes > 9) 
curDateTime = curDateTime + ":" + minutes; 
else 
curDateTime = curDateTime + ":0" + minutes; 
if (seconds > 9) 
curDateTime = curDateTime + ":" + seconds; 
else 
curDateTime = curDateTime + ":0" + seconds;*/ 
var weekday = ""; 
if (week == 0) 
weekday = "星期日"; 
else if (week == 1) 
weekday = "星期一"; 
else if (week == 2) 
weekday = "星期二"; 
else if (week == 3) 
weekday = "星期三"; 
else if (week == 4) 
weekday = "星期四"; 
else if (week == 5) 
weekday = "星期五"; 
else if (week == 6) 
weekday = "星期六"; 
curDateTime = curDateTime + " " + weekday; 
return curDateTime; 
}

2.显示农历代码:

Js代码

/*获取当前农历*/ 
function showCal(){ 
var D=new Date(); 
var yy=D.getFullYear(); 
var mm=D.getMonth()+1; 
var dd=D.getDate(); 
var ww=D.getDay(); 
var ss=parseInt(D.getTime() / 1000); 
if (yy<100) yy="19"+yy; 
return GetLunarDay(yy,mm,dd); 
} 

//定义全局变量 
var CalendarData=new Array(100); 
var madd=new Array(12); 
var tgString="甲乙丙丁戊己庚辛壬癸"; 
var dzString="子丑寅卯辰巳午未申酉戌亥"; 
var numString="一二三四五六七八九十"; 
var monString="正二三四五六七八九十冬腊"; 
var weekString="日一二三四五六"; 
var sx="鼠牛虎兔龙蛇马羊猴鸡狗猪"; 
var cYear,cMonth,cDay,TheDate; 
CalendarData = new Array(0xA4B,0x5164B,0x6A5,0x6D4,0x415B5,0x2B6,0x957,0x2092F,0x497,0x60C96,0xD4A,0xEA5,0x50DA9,0x5AD,0x2B6,0x3126E, 0x92E,0x7192D,0xC95,0xD4A,0x61B4A,0xB55,0x56A,0x4155B, 0x25D,0x92D,0x2192B,0xA95,0x71695,0x6CA,0xB55,0x50AB5,0x4DA,0xA5B,0x30A57,0x52B,0x8152A,0xE95,0x6AA,0x615AA,0xAB5,0x4B6,0x414AE,0xA57,0x526,0x31D26,0xD95,0x70B55,0x56A,0x96D,0x5095D,0x4AD,0xA4D,0x41A4D,0xD25,0x81AA5,0xB54,0xB6A,0x612DA,0x95B,0x49B,0x41497,0xA4B,0xA164B, 0x6A5,0x6D4,0x615B4,0xAB6,0x957,0x5092F,0x497,0x64B, 0x30D4A,0xEA5,0x80D65,0x5AC,0xAB6,0x5126D,0x92E,0xC96,0x41A95,0xD4A,0xDA5,0x20B55,0x56A,0x7155B,0x25D,0x92D,0x5192B,0xA95,0xB4A,0x416AA,0xAD5,0x90AB5,0x4BA,0xA5B, 0x60A57,0x52B,0xA93,0x40E95); 
madd[0]=0; 
madd[1]=31; 
madd[2]=59; 
madd[3]=90; 
madd[4]=120; 
madd[5]=151; 
madd[6]=181; 
madd[7]=212; 
madd[8]=243; 
madd[9]=273; 
madd[10]=304; 
madd[11]=334; 

function GetBit(m,n){ 
return (m>>n)&1; 
} 
//农历转换 
function e2c(){ 
TheDate= (arguments.length!=3) ? new Date() : new Date(arguments[0],arguments[1],arguments[2]); 
var total,m,n,k; 
var isEnd=false; 
var tmp=TheDate.getYear(); 
if(tmp<1900){ 
tmp+=1900; 
} 
total=(tmp-1921)*365+Math.floor((tmp-1921)/4)+madd[TheDate.getMonth()]+TheDate.getDate()-38; 

if(TheDate.getYear()%4==0&&TheDate.getMonth()>1) { 
total++; 
} 
for(m=0;;m++){ 
k=(CalendarData[m]<0xfff)?11:12; 
for(n=k;n>=0;n--){ 
if(total<=29+GetBit(CalendarData[m],n)){ 
isEnd=true; break; 
} 
total=total-29-GetBit(CalendarData[m],n); 
} 
if(isEnd) break; 
} 
cYear=1921 + m; 
cMonth=k-n+1; 
cDay=total; 
if(k==12){ 
if(cMonth==Math.floor(CalendarData[m]/0x10000)+1){ 
cMonth=1-cMonth; 
} 
if(cMonth>Math.floor(CalendarData[m]/0x10000)+1){ 
cMonth--; 
} 
} 
} 

function GetcDateString(){ 
var tmp=""; 
/*显示农历年:( 如:甲午(马)年 )*/ 
/*tmp+=tgString.charAt((cYear-4)%10); 
tmp+=dzString.charAt((cYear-4)%12); 
tmp+="("; 
tmp+=sx.charAt((cYear-4)%12); 
tmp+=")年 ";*/ 
if(cMonth<1){ 
tmp+="(闰)"; 
tmp+=monString.charAt(-cMonth-1); 
}else{ 
tmp+=monString.charAt(cMonth-1); 
} 
tmp+="月"; 
tmp+=(cDay<11)?"初":((cDay<20)?"十":((cDay<30)?"廿":"三十")); 
if (cDay%10!=0||cDay==10){ 
tmp+=numString.charAt((cDay-1)%10); 
} 
return tmp; 
} 

function GetLunarDay(solarYear,solarMonth,solarDay){ 
//solarYear = solarYear<1900?(1900+solarYear):solarYear; 
if(solarYear<1921 || solarYear>2020){ 
return ""; 
}else{ 
solarMonth = (parseInt(solarMonth)>0) ? (solarMonth-1) : 11; 
e2c(solarYear,solarMonth,solarDay); 
return GetcDateString(); 
} 
}

3.显示:

Js代码

$(function(){ 
var date = getCurrentDateTime(); 
var calendar = showCal(); 
$("#currentDate").text("今天是: " + date + "农历:" + calendar); 
});

显示今天的日期js代码(阳历和农历)

Javascript 相关文章推荐
JavaScript设置FieldSet展开与收缩
May 15 Javascript
JavaScript的递归之递归与循环示例介绍
Aug 05 Javascript
Vue无限滑动周选择日期的组件的示例代码
Jul 18 Javascript
微信小程序中使用wxss加载图片并实现动画效果
Aug 13 Javascript
vue-lazyload使用总结(推荐)
Nov 01 Javascript
Puppeteer 爬取动态生成的网页实战
Nov 14 Javascript
Vue从TodoList中学父子组件通信
Feb 05 Javascript
微信小程序生成二维码的示例代码
Mar 29 Javascript
vue-cli 项目打包完成后运行文件路径报错问题
Jul 19 Javascript
详解基于Vue/React项目的移动端适配方案
Aug 23 Javascript
js之ajax文件上传
May 13 Javascript
vue组件冲突之引用另一个组件出现组件不显示的问题
Apr 13 Vue.js
gridview生成时如何去掉style属性中的border-collapse
Sep 30 #Javascript
javascript学习笔记(四)function函数部分
Sep 30 #Javascript
javascript学习笔记(三)BOM和DOM详解
Sep 30 #Javascript
让人蛋疼的JavaScript语法特性
Sep 30 #Javascript
javascript学习笔记(二)数组和对象部分
Sep 30 #Javascript
javascript学习笔记(一)基础知识
Sep 30 #Javascript
js使用循环清空某个div中的input标签值
Sep 29 #Javascript
You might like
php ctype函数中文翻译和示例
2014/03/21 PHP
PHP版本常用的排序算法汇总
2015/12/20 PHP
PHP实现微信退款功能
2018/10/02 PHP
PHP 扩展Memcached命令用法实例总结
2020/06/04 PHP
javascript判断ie浏览器6/7版本加载不同样式表的实现代码
2011/12/26 Javascript
使用js简单实现了tree树菜单
2013/11/20 Javascript
Javascript中this的用法详解
2014/09/22 Javascript
JavaScript中的类与实例实现方法
2015/01/23 Javascript
JS实现漂亮的时间选择框效果
2016/08/20 Javascript
利用jQuery异步上传文件的插件用法详解
2017/07/19 jQuery
js判断节假日实例代码
2017/12/27 Javascript
layui扩展上传组件模拟进度条的方法
2019/09/23 Javascript
JS 数组基本用法入门示例解析
2020/01/16 Javascript
Quasar Input:type=&quot;number&quot; 去掉上下小箭头 实现加减按钮样式功能
2020/04/09 Javascript
python不带重复的全排列代码
2013/08/13 Python
Python中的is和id用法分析
2015/01/26 Python
Python的Flask框架中SQLAlchemy使用时的乱码问题解决
2015/11/07 Python
python动态网页批量爬取
2016/02/14 Python
浅谈python中对于json写入txt文件的编码问题
2018/06/07 Python
对dataframe进行列相加,行相加的实例
2018/06/08 Python
Python3.5集合及其常见运算实例详解
2019/05/01 Python
了解不常见但是实用的Python技巧
2019/05/23 Python
屏蔽Django admin界面添加按钮的操作
2020/03/11 Python
windows上彻底删除jupyter notebook的实现
2020/04/13 Python
用Python开发app后端有优势吗
2020/06/29 Python
Python装饰器如何实现修复过程解析
2020/09/05 Python
python 实现音频叠加的示例
2020/10/29 Python
css3的focus-within选择器的使用
2020/05/11 HTML / CSS
Vans英国官方网站:美国南加州的原创极限运动潮牌
2017/01/20 全球购物
Book Depository美国:全球领先的专业网上书店之一
2019/08/14 全球购物
爱心捐助倡议书
2014/05/19 职场文书
雷人标语集锦
2014/06/19 职场文书
2015年企业工作总结范文
2015/04/28 职场文书
css position fixed 左右双定位的实现代码
2021/04/29 HTML / CSS
vue项目中的支付功能实现(微信支付和支付宝支付)
2022/02/18 Vue.js
SQL Server表分区降低运维和维护成本
2022/04/08 SQL Server