当前位置: 代码迷 >> JavaScript >> js 获取当前时间,以及js中日期转换成字符串判断有关问题
  详细解决方案

js 获取当前时间,以及js中日期转换成字符串判断有关问题

热度:159   发布时间:2012-10-30 16:13:36.0
js 获取当前时间,以及js中日期转换成字符串判断问题

<script>
?function time_show()?
?{?

?? 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( );?????? //获取日期与时间
??? var minute = myDate.getMinutes();
??? var seconds = myDate.getSeconds();
??? if (parseInt(minute) < 10){
??? ??? minute = "0" + minute;
??? }
??? if (parseInt(seconds) < 10){
??? ??? seconds = "0" + seconds;
??? }
?? $("#time").html(myDate.getFullYear()+"-"+(myDate.getMonth()+1)+"-"+myDate.getDate()+" "+myDate.getHours()+":"+ minute +":"+ seconds);
?}
?setInterval("time_show()",1000);?
</script>

?

?

///比较两个日期的大小d1,d2格式为2011-10-18(或者2010-10-18 10:29:6);返回数result>0说明d1>d2 result== 0说明d1==d2 result<0说明d1<d2
??? ??? ??? ??? function dateDiff(d1,d2){
??? ??? ??? ???????? var result = Date.parse(d1.replace(/-/g,"/"))- Date.parse(d2.replace(/-/g,"/"));
??? ??? ??? ???????? return result;
??? ??? ??? ? ??? }
??? ??? ??? ? ???
??? ??? ??? ? ??? // 将日期类型转换成字符串型格式yyyy-MM-dd hh:mm
??? ??? ??? ??? function ChangeTimeToString(DateIn)
??? ??? ??? ??? {
??? ??? ??? ??? ??? var Year=0;
??? ??? ??? ??? ??? var Month=0;
??? ??? ??? ??? ??? var Day=0;
??? ??? ??? ??? ??? var Hour = 0;
??? ??? ??? ??? ??? var Minute = 0;
??? ??? ??? ??? ??? var CurrentDate="";
??? ??? ??? ???
??? ??? ??? ??? ??? //初始化时间
??? ??? ??? ??? ??? Year????? = DateIn.getFullYear();
??? ??? ??? ??? ??? Month???? = DateIn.getMonth()+1;
??? ??? ??? ??? ??? Day?????? = DateIn.getDate();
??? ??? ??? ??? ??? Hour????? = DateIn.getHours();
??? ??? ??? ??? ??? Minute??? = DateIn.getMinutes();
??? ??? ??? ???
??? ??? ??? ??? ??? CurrentDate = Year + "-";
??? ??? ??? ??? ??? if (Month >= 10 )
??? ??? ??? ??? ??? {
??? ??? ??? ??? ??????? CurrentDate = CurrentDate + Month + "-";
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? else
??? ??? ??? ??? ??? {
??? ??? ??? ??? ??????? CurrentDate = CurrentDate + "0" + Month + "-";
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? if (Day >= 10 )
??? ??? ??? ??? ??? {
??? ??? ??? ??? ??????? CurrentDate = CurrentDate + Day ;
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? else
??? ??? ??? ??? ??? {
??? ??? ??? ??? ??????? CurrentDate = CurrentDate + "0" + Day ;
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??
??? ??? ??? ??? ???? if(Hour >=10)
??? ??? ??? ??? ??? {
??? ??? ??? ??? ??????? CurrentDate = CurrentDate + " " + Hour ;
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? else
??? ??? ??? ??? ??? {
??? ??? ??? ??? ??????? CurrentDate = CurrentDate + " 0" + Hour ;
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? if(Minute >=10)
??? ??? ??? ??? ??? {
??? ??? ??? ??? ??????? CurrentDate = CurrentDate + ":" + Minute ;
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? else
??? ??? ??? ??? ??? {
??? ??? ??? ??? ??????? CurrentDate = CurrentDate + ":0" + Minute ;
??? ??? ??? ??? ??? }????
??? ??? ??? ??? ??? return CurrentDate;
??? ??? ??? ??? }

  相关解决方案