当前位置: 首页 > news >正文

js显示日期和时间

显示 年月日+星期

<script type="text/javascript">

function showDate()

{        

    var day="";        

    var month="";        

    var ampm="";        

    var ampmhour="";        

    var myweekday="";        

    var year="";        

    mydate=new Date();        

    myweekday=mydate.getDay();        

    mymonth=mydate.getMonth()+1;        

    myday= mydate.getDate();        

    myyear= mydate.getYear();        

    year=(myyear > 200) ? myyear : 1900 + myyear;        

    if(myweekday == 0)         

        weekday=" 星期日";        

    else if(myweekday == 1)        

        weekday=" 星期一";        

    else if(myweekday == 2)        

        weekday=" 星期二";        

    else if(myweekday == 3)        

        weekday=" 星期三";        

    else if(myweekday == 4)         

        weekday=" 星期四";        

    else if(myweekday == 5)        

        weekday=" 星期五";       

    else if(myweekday == 6)        

        weekday=" 星期六";        

    document.write(year+""+mymonth+""+myday+""+weekday);

}

showDate();

</script>

<script type="text/javascript">

today=new Date();

function initArray()

{

   this.length=initArray.arguments.length

   for(var i=0;i<this.length;i++)

   {

     this[i+1]=initArray.arguments[i];

   }

}

var d=new initArray(" 星期日"," 星期一"," 星期二"," 星期三"," 星期四"," 星期五"," 星期六");

document.write(today.getYear(),"","",today.getMonth()+1,"",today.getDate(),"",d[today.getDay()+1]);

</script>

 

 

显示当前日期时间[不断变化]

 

 

<div id="Clock" align="center" style="font-size: 12px; color:#000000"></div> 

<script>

function tick() {

var hours, minutes, seconds, xfile;

var intHours, intMinutes, intSeconds;

var today, theday;

today = new Date();

function initArray(){

this.length=initArray.arguments.length

for(var i=0;i<this.length;i++)

this[i+1]=initArray.arguments[i] }

var d=new initArray(

"星期日",

"星期一",

"星期二",

"星期三",

"星期四",

"星期五",

"星期六");

theday = today.getYear()+"" + [today.getMonth()+1]+"" +today.getDate() + d[today.getDay()+1];

intHours = today.getHours();

intMinutes = today.getMinutes();

intSeconds = today.getSeconds();

if (intHours == 0) {

hours = "12:";

xfile = "午夜";

} else if (intHours < 12) {

hours = intHours+":";

xfile = "上午";

} else if (intHours == 12) {

hours = "12:";

xfile = "正午";

} else {

intHours = intHours - 12

hours = intHours + ":";

xfile = "下午";

}

if (intMinutes < 10) {

minutes = "0"+intMinutes+":";

} else {

minutes = intMinutes+":";

}

if (intSeconds < 10) {

seconds = "0"+intSeconds+" ";

} else {

seconds = intSeconds+" ";

}

timeString = theday+xfile+hours+minutes+seconds;

Clock.innerHTML = timeString;

window.setTimeout("tick();", 100);

}

window.onload = tick;

</script>

 

或者

 

<script language="JavaScript" type="text/javascript">

calendar = new Date();

day = calendar.getDay();

month = calendar.getMonth();

date = calendar.getDate();

year = calendar.getYear();

if (year< 100) year = 1900 + year;

cent = parseInt(year/100);

g = year % 19;

k = parseInt((cent - 17)/25);

i = (cent - parseInt(cent/4) - parseInt((cent - k)/3) + 19*g + 15) % 30;

i = i - parseInt(i/28)*(1 - parseInt(i/28)*parseInt(29/(i+1))*parseInt((21-g)/11));

j = (year + parseInt(year/4) + i + 2 - cent + parseInt(cent/4)) % 7;

l = i - j;

emonth = 3 + parseInt((l + 40)/44);

edate = l + 28 - 31*parseInt((emonth/4));

emonth--;

var dayname = new Array ("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");

var monthname =

new Array ("1","2","3","4","5","6","7","8","9","10","11","12" );

document.write(year +"");

document.write(monthname[month]);

document.write(date + ""+" ");

document.write(dayname[day]);

document.write("<span id='clock'></span>");

var now,hours,minutes,seconds,timeValue;

function showtime(){

now = new Date();

hours = now.getHours();

minutes = now.getMinutes();

seconds = now.getSeconds();

timeValue = (hours >= 12) ? " 下午" : " 上午";

timeValue += ((hours > 12) ? hours - 12 : hours) + "";

timeValue += ((minutes <10)?"0":"") + minutes+"";

timeValue += ((seconds <10)?"0":"") + seconds+"";

clock.innerHTML = timeValue;

setTimeout("showtime()",100);

}

showtime();

</script>

 

精简的代码

 

<script language="javascript" type="text/javascript"> 

        setInterval("timer.innerHTML=new Date().toLocaleString()+'&nbsp;&nbsp;星期'+'日一二三四五六'.charAt(new Date().getDay());",1000);      

</script>

<span id="timer"></span>

 

 

相关文章:

  • Visual Studio 2008 试用版升级
  • js屏蔽按键
  • 80后出师表
  • Ajax使用POST提交中文乱码问题(转)
  • css网页背景渐变
  • 运行代码,复制代码,保存代码
  • IE6 IE7 IE8 FF浏览器的CSS兼容问题(转)
  • ASP 编程中 20 个非常有用的例子
  • position 绝对定位的问题
  • phpmyadmin建表出错 #1064 - You have an error in your SQL syntax;
  • 一个女程序员的征婚信息
  • :“Failed to access IIS metabase”解决方法
  • 用户***/aspnet登陆失败
  • sql 2005拒绝了对对象 'xxx' (数据库 'xxx',架构 'dbo')的 SELECT 权限
  • 用min-height实现最小高度(兼容IE6、IE7、FF)
  • [Vue CLI 3] 配置解析之 css.extract
  • 【5+】跨webview多页面 触发事件(二)
  • codis proxy处理流程
  • ES6之路之模块详解
  • ESLint简单操作
  • java小心机(3)| 浅析finalize()
  • JS数组方法汇总
  • Mithril.js 入门介绍
  • mongodb--安装和初步使用教程
  • mysql外键的使用
  • python大佬养成计划----difflib模块
  • Spark RDD学习: aggregate函数
  • Web标准制定过程
  • Yii源码解读-服务定位器(Service Locator)
  • 飞驰在Mesos的涡轮引擎上
  • 高程读书笔记 第六章 面向对象程序设计
  • 基于 Ueditor 的现代化编辑器 Neditor 1.5.4 发布
  • 简单实现一个textarea自适应高度
  • 简单数学运算程序(不定期更新)
  • 聊聊directory traversal attack
  • 排序(1):冒泡排序
  • 如何进阶一名有竞争力的程序员?
  • 实现简单的正则表达式引擎
  • nb
  • ionic异常记录
  • Play Store发现SimBad恶意软件,1.5亿Android用户成受害者 ...
  • 数据库巡检项
  • # Python csv、xlsx、json、二进制(MP3) 文件读写基本使用
  • ###51单片机学习(2)-----如何通过C语言运用延时函数设计LED流水灯
  • (C语言)fread与fwrite详解
  • (Repost) Getting Genode with TrustZone on the i.MX
  • (zt)最盛行的警世狂言(爆笑)
  • (八)五种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (动手学习深度学习)第13章 计算机视觉---图像增广与微调
  • (每日持续更新)jdk api之FileReader基础、应用、实战
  • (转)Google的Objective-C编码规范
  • (转)创业的注意事项
  • (总结)Linux下的暴力密码在线破解工具Hydra详解
  • .NET Core 将实体类转换为 SQL(ORM 映射)
  • .net framework profiles /.net framework 配置