js获取当前日期时间

时间:2021-02-12 18:06:44
<script>
var myDate = new Date();
    myDate.getYear();       //获取当前年份(2位)
//alert(myDate.getYear()) //2009
    myDate.getFullYear();   //获取完整的年份(4位,1970-????)
//alert(myDate.getFullYear()); 2009
    myDate.getMonth();      //获取当前月份(0-11,0代表1月)
//alert(myDate.getMonth()); //7(实际是8月)
    myDate.getDate();       //获取当前日(1-31)
//alert( myDate.getDate()); //13
    myDate.getDay();        //获取当前星期X(0-6,0代表星期天)
//alert(myDate.getDay()); //4 代表星期四
    myDate.getTime();       //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours();      //获取当前小时数(0-23)
//alert(myDate.getHours()); //9 代表9点
    myDate.getMinutes();    //获取当前分钟数(0-59)
//alert(myDate.getMinutes()); //45 分
    myDate.getSeconds();    //获取当前秒数(0-59)
//alert(myDate.getSeconds()); //40 秒数
    myDate.getMilliseconds();   //获取当前毫秒数(0-999)
    myDate.toLocaleDateString();    //获取当前日期
    var mytime=myDate.toLocaleTimeString();    //获取当前时间
//alert(mytime);//9:40:18
    myDate.toLocaleString( );       //获取日期与时间
//alert( myDate.toLocaleString( )); //2009年8月13日 9:40:58

</script>

 

<script>
function curDateTime() {
   var d = new Date();
   var year = d.getYear();
   var month = d.getMonth() + 1;
   var date = d.getDate();
   var day = d.getDay();
   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;

   //myform.kprq.value = curDateTime;
   alert("当前日期"+curDateTime);
   document.getElementByIdx_x("date").value=curDateTime;

}
</script>

 

function curDateTime() {
       var d = new Date();
       var year = d.getFullYear();
       var month = d.getMonth() + 1;
       var date = d.getDate();
       var day = d.getDay();
       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;
       return curDateTime;
 }

 

 

<script type="text/javascript">
var birthday = new Date("July 21, 1983 01:15:00")
document.write(birthday.getDate())
</script>

输出:21

 js中获取时间new Date()详细介绍

JS获取当前时间戳的方法-JavaScript 获取当前时间戳

JavaScript 获取当前时间戳:
第一种方法:

var timestamp =Date.parse(new Date());
结果:1280977330000
第二种方法:

var timestamp =(new Date()).valueOf();
结果:1280977330748

第三种方法:

var timestamp=new Date().getTime();
结果:1280977330748

第一种:获取的时间戳是把毫秒改成000显示,

第二种和第三种是获取了当前毫秒的时间戳。

 将字符串形式的日期转换成日期对象

var strTime="2011-04-16"; //字符串日期格式           
var date= new Date(Date.parse(strTime.replace(/-/g,   "/"))); //转换成Data();

var month=date.getMonth()+1; //获取当前月份

 

//截至日期
var dateSlice = that.sDate.split('-');
var newDate = new Date(parseInt(dateSlice[0]), parseInt(dateSlice[1])+1, parseInt(dateSlice[2]));
that.eDate = typeof (eDate) === 'undefined' ? that.dateToString(newDate) : eDate;

 

this.dateToString = function (date) {
    var dateStr = date.getDate().toString();
    if (dateStr.length === 1) {
            dateStr = '0' + dateStr;
    }
    return date.getFullYear() + "-" + that.short_month_names[date.getMonth()] + "-" + dateStr;
 };
this.short_month_names = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"];

Date对象:http://www.cnblogs.com/polk6/p/4156595.html