`
asd14828
  • 浏览: 30063 次
  • 性别: Icon_minigender_1
  • 来自: 太原
社区版块
存档分类
最新评论

Js得到Date 操作

阅读更多
<SCRIPT LANGUAGE="JavaScript">
var myDate = new Date();
myDate.getYear();       //获取当前年份(2位)
myDate.getFullYear();   //获取完整的年份(4位,1970-????)
myDate.getMonth();      //获取当前月份(0-11,0代表1月)
myDate.getDate();       //获取当前日(1-31)
myDate.getDay();        //获取当前星期X(0-6,0代表星期天)
myDate.getTime();       //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours();      //获取当前小时数(0-23)
myDate.getMinutes();    //获取当前分钟数(0-59)
myDate.getSeconds();    //获取当前秒数(0-59)
myDate.getMilliseconds();   //获取当前毫秒数(0-999)
myDate.toLocaleDateString();    //获取当前日期
var mytime=myDate.toLocaleTimeString();    //获取当前时间
myDate.toLocaleString( );       //获取日期与时间
if (mytime<"23:30:00"){
   alert(mytime);
}
</SCRIPT>

2.<% //日期转换
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
java.util.Date currentTime = new java.util.Date();//得到当前系统时间 
String str_date1 = formatter.format(currentTime); //将日期时间格式化 
String str_date2 = currentTime.toString(); //将Date型日期时间转换成字符串形式 
%>

1. 2008年11月13日 星期四 上午 10:13:46 
<div id="Clock" align="center" style="font-size: 15px; 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>


2. 2008年11月13 星期四 丙戌年 二月十五
<script language="JavaScript">
var bsYear;
var bsDate;
var bsWeek;
var arrLen=8; //数组长度
var sValue=0; //当年的秒数
var dayiy=0; //当年第几天
var miy=0; //月份的下标
var iyear=0; //年份标记
var dayim=0; //当月第几天
var spd=86400; //每天的秒数

var year1999="30;29;29;30;29;29;30;29;30;30;30;29"; //354
var year2000="30;30;29;29;30;29;29;30;29;30;30;29"; //354
var year2001="30;30;29;30;29;30;29;29;30;29;30;29;30"; //384
var year2002="30;30;29;30;29;30;29;29;30;29;30;29"; //354
var year2003="30;30;29;30;30;29;30;29;29;30;29;30"; //355
var year2004="29;30;29;30;30;29;30;29;30;29;30;29;30"; //384
var year2005="29;30;29;30;29;30;30;29;30;29;30;29"; //354
var year2006="30;29;30;29;30;30;29;29;30;30;29;29;30";

var month1999="正月;二月;三月;四月;五月;六月;七月;八月;九月;十月;十一月;十二月"
var month2001="正月;二月;三月;四月;闰四月;五月;六月;七月;八月;九月;十月;十一月;十二月"
var month2004="正月;二月;闰二月;三月;四月;五月;六月;七月;八月;九月;十月;十一月;十二月"
var month2006="正月;二月;三月;四月;五月;六月;七月;闰七月;八月;九月;十月;十一月;十二月"
var Dn="初一;初二;初三;初四;初五;初六;初七;初八;初九;初十;十一;十二;十三;十四;十五;十六;十七;十八;十九;二十;廿一;廿二;廿三;廿四;廿五;廿六;廿七;廿八;廿九;三十";

var Ys=new Array(arrLen);
Ys[0]=919094400;Ys[1]=949680000;Ys[2]=980265600;
Ys[3]=1013443200;Ys[4]=1044028800;Ys[5]=1074700800;
Ys[6]=1107878400;Ys[7]=1138464000;

var Yn=new Array(arrLen); //农历年的名称
Yn[0]="己卯年";Yn[1]="庚辰年";Yn[2]="辛巳年";
Yn[3]="壬午年";Yn[4]="癸未年";Yn[5]="甲申年";
Yn[6]="乙酉年";Yn[7]="丙戌年";
var D=new Date();
var yy=D.getYear();
var mm=D.getMonth()+1;
var dd=D.getDate();
var ww=D.getDay();
if (ww==0) ww="<font color=RED>星期日</font>";
if (ww==1) ww="星期一";
if (ww==2) ww="星期二";
if (ww==3) ww="星期三";
if (ww==4) ww="星期四";
if (ww==5) ww="星期五";
if (ww==6) ww="<font color=green>星期六</font>";
ww=ww;
var ss=parseInt(D.getTime() / 1000);
if (yy<100) yy="19"+yy;

for (i=0;i<arrLen;i++)
if (ss>=Ys[i]){
iyear=i;
sValue=ss-Ys[i]; //当年的秒数
}
dayiy=parseInt(sValue/spd)+1; //当年的天数

var dpm=year1999;
if (iyear==1) dpm=year2000;
if (iyear==2) dpm=year2001;
if (iyear==3) dpm=year2002;
if (iyear==4) dpm=year2003;
if (iyear==5) dpm=year2004;
if (iyear==6) dpm=year2005;
if (iyear==7) dpm=year2006;
dpm=dpm.split(";");

var Mn=month1999;
if (iyear==2) Mn=month2001;
if (iyear==5) Mn=month2004;
if (iyear==7) Mn=month2006;
Mn=Mn.split(";");

var Dn="初一;初二;初三;初四;初五;初六;初七;初八;初九;初十;十一;十二;十三;十四;十五;十六;十七;十八;十九;二十;廿一;廿二;廿三;廿四;廿五;廿六;廿七;廿八;廿九;三十";
Dn=Dn.split(";");

dayim=dayiy;

var total=new Array(13);
total[0]=parseInt(dpm[0]);
for (i=1;i<dpm.length-1;i++) total[i]=parseInt(dpm[i])+total[i-1];
for (i=dpm.length-1;i>0;i--)
if (dayim>total[i-1]){
dayim=dayim-total[i-1];
miy=i;
}
bsWeek=ww;
bsDate=yy+"年"+mm+"月";
bsDate2=dd;
bsYear="农历"+Yn[iyear];
bsYear2=Mn[miy]+Dn[dayim-1];
if (ss>=Ys[7]||ss<Ys[0]) bsYear=Yn[7];
function time(){
document.write("<table border=''0'' style=''font-size: 8pt; font-family:Tahoma'' cellspacing=''0'' width=''90'' bordercolor=''#cccccc'' height=''110'' cellpadding=''0''");
document.write("<tr><td align=''center'' style=''border: 1 solid #0099ff;padding-top:4px''><b><font color=#3366cc>"+bsDate+"</font><br><span style=''font-family: Arial Black;font-size:18pt;color:#FF0000''>"+bsDate2+"</span><br><br><span style=''FONT-SIZE: 10.5pt;color:#000000''>");
document.write(bsWeek+"</span><br>"+"<hr width=''60'' ></b><font color=#666666>");
document.write(bsYear+"<br>"+bsYear2+"</td></tr></table>");
}
</script>
<script>time()</script>

3. November 13,2008 10:15:58 
<SCRIPT language=javascript>
<!--
function Year_Month(){ 
var now = new Date(); 
var yy = now.getYear(); 
var mm = now.getMonth(); 
var mmm=new Array();
mmm[0]="January";
mmm[1]="February ";
mmm[2]="March";
mmm[3]="April";
mmm[4]="May";
mmm[5]="June";
mmm[6]="July";
mmm[7]="August";
mmm[8]="September";
mmm[9]="October";
mmm[10]="November";
mmm[11]="December";
mm=mmm[mm];
return(mm); 
}
function thisYear(){ 
var now = new Date(); 
var yy = now.getYear(); 
return(yy); 
}
function Date_of_Today(){ 
var now = new Date(); 
return(now.getDate());
}
function CurentTime(){ 
var now = new Date(); 
var hh = now.getHours(); 
var mm = now.getMinutes(); 
var ss = now.getTime() % 60000; 
ss = (ss - (ss % 1000)) / 1000; 
var clock = hh+':'; 
if (mm < 10) clock += '0'; 
clock += mm+':'; 
if (ss < 10) clock += '0'; 
clock += ss; 
return(clock); 
} 
function refreshCalendarClock(){ 
document.all.calendarClock1.innerHTML = Year_Month(); 
document.all.calendarClock2.innerHTML = Date_of_Today(); 
document.all.calendarClock3.innerHTML =thisYear(); 
document.all.calendarClock4.innerHTML = CurentTime(); }
document.write('<font id="calendarClock1" > </font>&nbsp;');
document.write('<font id="calendarClock2" > </font>,');
document.write('<font id="calendarClock3" > </font>&nbsp;');
document.write('<font id="calendarClock4" > </font>');
setInterval('refreshCalendarClock()',1000);
//-->
</SCRIPT>
<script language="javascript" src="./script/date.js"></script>

4. 现在时间:10:16:45 AM
<SCRIPT language=javascript>
<!--
function show5(){
if(!document.layers&&!document.all)
   return
   var Digital=new Date()
   var hours=Digital.getHours()
   var minutes=Digital.getMinutes()
   var seconds=Digital.getSeconds()
   var dn="AM"
   if(hours>12){dn="PM"
   hours=hours-12
   }if(hours==0)
   hours=12
   if(minutes<=9)
   minutes="0"+minutes
   if(seconds<=9)
   seconds="0"+seconds
   //change font size here to your desire
   myclock="<font    color=blue>现在时间:"+hours+":"+minutes+":"+seconds+" "+dn+"</font>";
   if(document.layers){
   document.layers.liveclock.document.write(myclock)
   document.layers.liveclock.document.close()
   }else if(document.all)
   liveclock.innerHTML=myclock
   setTimeout("show5()",1000)
}
//-->
</SCRIPT>
<body onLoad=show5()>
<span id=liveclock style=position:absolute;left:250px;top:122px;; width: 109px; height: 15px> 
</span>

5. 10:11:12
<head>
<script language=javaScript>
<!--// 
function clockon() {
thistime= new Date()
var hours=thistime.getHours()
var minutes=thistime.getMinutes()
var seconds=thistime.getSeconds()
if (eval(hours) <10) {hours="0"+hours}
if (eval(minutes) < 10) {minutes="0"+minutes}
if (seconds < 10) {seconds="0"+seconds}
thistime = hours+":"+minutes+":"+seconds
if(document.all) {
bgclocknoshade.innerHTML=thistime
bgclockshade.innerHTML=thistime
}
if(document.layers) {
document.bgclockshade.document.write('<div id="bgclockshade" style="position:absolute;visibility:visible;font-family:Verdana;color:FFAAAAA;font-size:120px;top:10px;left:152px">'+thistime+'</div>')
document.bgclocknoshade.document.write('<div id="bgclocknoshade" style="position:absolute;visibility:visible;font-family:Verdana;color:DDDDDD;font-size:120px;top:10px;left:150px">'+thistime+'</div>')
document.close()
}
var timer=setTimeout("clockon()",200)
}

//-->
</script>
</head>
<body bgcolor="#ffffff" onLoad="clockon()">
<div id="bgclockshade" style="position:absolute;visibility:visible;font-family:Arial;color:333333;font-size:120px;top:102px;left:152px"></div>
<div id="bgclocknoshade" style="position:absolute;visibility:visible;font-family:Arial;color:0099ff;font-size:120px;top:100px;left:150px"></div>
<div id="mainbody" style="position:absolute; visibility:visible">
</div>
</body>

分享到:
评论

相关推荐

    javascript转换日期字符串为Date日期对象的方法.docx

    很多时候,我们需要将用户输入或其他来源得到的日期字符串转换为 `Date` 对象,以便进行进一步的计算或操作。本文将详细介绍几种常用的转换方法,并提供示例代码帮助理解。 #### 方法一:手动解析 这种方法适用于...

    javascript date类型转成OLE(double)

    Date对象的方法提供了对日期和时间的多种操作,包括设置和获取年份、月份、日期、小时、分钟、秒等。 **2. OLE DATE类型** OLE DATE类型是一种用于表示日期时间的标准格式,主要用于COM组件之间以及COM组件与非COM...

    wui-date.7z

    【wui-date】是一个专为前端开发者设计的轻量级日期时间选择器组件,它以其简洁的界面和高效的性能在Web应用中得到了广泛的应用。基于流行的JavaScript库jQuery和AngularJS框架,wui-date使得在网页上添加交互式、...

    JS简单实现String转Date的方法

    `Date`对象是JavaScript内置的对象之一,用于处理日期和时间。有时我们需要将字符串转换为`Date`对象,以便进行进一步的操作,如计算日期差、格式化日期等。本文将深入探讨如何使用JavaScript将字符串转换为`Date`...

    Date Object in Javascript.zip

    JavaScript中的Date对象还支持时间差计算,通过`getTime()`方法获取日期对象表示的毫秒数,然后用减法操作可以得到两个日期之间的差值。例如: ```javascript var date1 = new Date("2022-01-01"); var date2 = new ...

    Date时间操作工具类

    Date时间操作工具类 js: 1.一个日期时间加上分钟数,得到一个新的日期时间 2.判断当前时间是否在时间段之内 3.根据默认的格式格式化时间 4.根据指定的格式格式化时间 5.改变月份 6.改变天数 7.计算天数间隔 8.Date转...

    JavaScript Date对象详解

    - 比较两个Date对象:通过减法操作可以得到两个Date对象的时间差,从而判断先后顺序。 由于JavaScript中的时间总是以UTC时间存储,而在显示时则转换为本地时区时间,因此我们需要了解时区相关的处理。在创建Date...

    Javascript之Date对象详解

    JavaScript中的Date对象是用于处理日期和时间的核心构造函数,提供了丰富的功能来操作和展示日期。以下是对Date对象的详细说明: 1. 创建Date对象: - 默认创建:`var myDate = new Date();` 这将创建一个表示当前...

    javascript中Date对象应用之简易日历实现_.docx

    3. **计算月份的天数**:调用`date.getMonthDays()`(自定义方法,因为JavaScript的Date对象不直接提供获取月份总天数的方法)以得到该月的总天数。 4. **初始化日历**:清除`.dayBox`内的所有日期,然后根据月份...

    JQ JS javascript new Data() 获取 指定日期 的 后一天

    在JavaScript编程语言中,`Date`对象是处理日期和时间的核心工具。`new Date()`构造函数用于创建一个表示当前日期和时间的新`Date`对象。然而,在这个特定的场景下,我们关注的是如何使用`Date`对象来获取指定日期...

    js Date概念详细介绍

    Date对象是JavaScript编程中常用的功能之一,无论是在前端还是后端开发中,日期时间的操作都非常常见。掌握这些方法和属性能帮助开发者有效地处理日期和时间数据。例如,开发一个基于当前时间的日历应用,或者计算两...

    date2obj在JavaScript中将日期解析为简单对象

    在JavaScript中,日期通常被表示为`Date`对象,但有时我们可能需要将日期转换为更易于操作的简单JavaScript对象。这就是`date2obj`函数的作用。这个函数可以将日期对象解析成一个包含年、月、日、小时、分钟、秒等...

    JavaScript DateFormat

    在JavaScript中,`Date`对象是内置的,可以用来创建、操作和格式化日期。不过,原生的`Date`对象提供的方法(如`toLocaleString()`、`toDateString()`等)并不总是能满足所有需求,因此开发者经常需要自己编写或使用...

    JavaScript中的比较操作符&gt;、&lt;、&gt;=、&lt;=介绍

    在JavaScript编程语言中,比较操作符是比较两个变量或表达式的值并返回一个布尔值(true或false)的运算符。JavaScript中常用的比较操作符包括大于(&gt;)、小于(&lt;)、大于等于(&gt;=)、小于等于()。这些操作符可以...

    JS 获得两个时间间隔天数

    总的来说,JavaScript中的日期操作是通过Date对象完成的,通过计算两个日期之间的毫秒差并进行转换,我们可以轻松地获取两个日期之间的天数差。这对于开发涉及到日期和时间的Web应用是非常有用的。

    JavaScript利用Date实现简单的倒计时实例

    JavaScript中的Date对象是用于处理日期和时间的核心工具,它提供了丰富的内置方法来创建、操作和格式化日期。在本文中,我们将重点讨论如何利用Date对象实现一个简单的倒计时功能,以及在倒计时结束后使按钮变为可...

    js日期相加

    `js日期相加`是指在JavaScript中如何对两个日期对象进行操作,通常是将它们的时间值相加,以得到一个新日期。这种操作在处理日历系统、事件调度或者任何需要计算日期差值的场景中都非常常见。下面将详细探讨...

    js自动获得日期

    在JavaScript(JS)中,获取日期是一项常见的任务,可以用于创建动态时间戳、记录日志、处理表单验证等场景。本篇文章将深入探讨如何在JavaScript中自动获取日期,并介绍相关的日期对象和方法。 JavaScript提供了...

    JavaScript Date对象应用实例分享

    JavaScript Date对象是JavaScript提供的用于处理日期和时间的一个内置对象。它允许你执行各种操作,比如获取当前日期和时间、计算日期之间的差异以及创建特定的日期对象。这篇文章通过实例分享了如何利用JavaScript ...

    详解JavaScript的Date对象(制作简易钟表)

    JavaScript的Date对象是用于处理和操作日期与时间的内置类型,它提供了丰富的功能来获取、设置和格式化日期。Date对象允许我们创建、比较、计算日期,并且可以以多种方式显示日期和时间。 首先,创建一个新的Date...

Global site tag (gtag.js) - Google Analytics