>  기사  >  웹 프론트엔드  >  자바스크립트에서 Date 객체 사용 요약

자바스크립트에서 Date 객체 사용 요약

高洛峰
高洛峰원래의
2016-12-06 10:20:331135검색

JSON 날짜를 JS 날짜로 변환하면 날짜 유형이 JSON으로 변환된 후 반환되는 데이터가 다음과 유사하다는 것을 알 수 있습니다.

 /Date(1379944571737)/

 하지만 이런 종류 날짜는 직접 표시할 수 없습니다. 이것이 무엇을 의미하는지 아무도 모르기 때문에 여기에 JSON 날짜를 JS 날짜로 변환하는 방법이 있습니다.

function ConvertJSONDateToJSDate(jsondate) {
 var date = new Date(parseInt(jsondate.replace("/Date(", "").replace(")/", ""), 10));
 return date;
}


은 날짜 변환을 위한 두 가지 날짜 형식을 제공합니다.

//yyyy-MM-dd
function getDate(date) {
 var year = date.getFullYear();
 var month = date.getMonth() + 1;
 var day = date.getDate();
 return year + "-" + month + "-" + day ;
}
//yyyy-MM-dd HH:mm:SS
function getDateTime(date) {
 var year = date.getFullYear();
 var month = date.getMonth() + 1;
 var day = date.getDate();
 var hh = date.getHours();
 var mm = date.getMinutes();
 var ss = date.getSeconds();
 return year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss;
}


문자열을 Date 객체로 변환하는 방법:

var str = "2012-12-12";
var date = new Date(str);  //字符串转换为Date对象
document.write(date.getFullYear());  //然后就可以使用Date对象的方法输出年份了


1. Date.getDate () 날짜를 반환합니다. 날짜 개체의 해당 월입니다.

var date = new Date();  //2012-12-19
document.write(date.getDate());  //返回 19 是19号


2. Date.getDay() 날짜에서 요일을 반환합니다(일요일 0~6)

var date = new Date();
document.write(date.getDay());  //3 星期3


3. Date.getFulYead() 2012와 같은 연도를 반환합니다.

var date = new Date();
document.write(date.getFullYear());  //返回2012,2012年


4. Date.getHours() 날짜의 시간(0~23)을 반환합니다.

var date = new Date();
document.write(date.getHours());  //返回23,晚上11点


5. Date.getMilliseconds() 날짜의 밀리초 수를 반환합니다.

var date = new Date();
document.write(date.getMilliseconds());  //返回27  当前是xx年,xx月,xx点,xx分,xx秒,xx毫秒的毫秒


6. Date.getMinutes() 날짜 0~59의 분 수를 반환합니다.

var date = new Date();
document.write(date.getMinutes());  //2012-12-19 23:22  返回22,12点22分


7.Date.getMonth ()  // 날짜의 개월수를 반환, 반환값은 0(1월)~11(12월)

var date = new Date();
document.write(date.getMonth());  //2012-12-19  此处返回11,注意此处与通常理解有些偏差,1月份返回是0,12月返回是11


8. Date.getSeconds()   //날짜 설명 반환

var date = new Date();
document.write(date.getSeconds());·//返回34,2012-12-19 23:27:34  27分34秒


9. /날짜 객체를 밀리초 형식의

var date = new Date();
document.write(date.getTime());  //返回1355930928466  返回值是1970-01-01 午夜到当前时间的毫秒数。

반환으로 변환


10. Date.getTimezoneOffset() // GMT 시간과 현지 시간의 차이 시간은 분 단위로 표시됩니다.

var date = new Date();
document.write(date.getTimezoneOffset());  //返回-480  实际上这个函数获取的是javascript运行于哪个时区。单位是分钟。


11. Date.getUTCDate() // Date 객체의 날짜 값을 반환합니다(글로벌 시간). )

var date = new Date();
document.write(date.getUTCDate());  //返回19  19号


12. Date.getUTCDay() // Date 객체의 요일을 반환합니다(글로벌 시간)

var date = new Date();
document.write(date.getUTCDay());  //返回3  星期3


13. Date.getUTCFulYear() // 연도를 Date로 4자리 반환(예: 2012, (글로벌 시간))

var date = new Date();
document.write(date.getUTCFullYear());  //返回2012


14.Date.getUTCHours() //Date 개체의 시간(현재 시간)을 반환합니다. 마지막으로 getHours()와 다른 것이 있는데, 이는 시차와 관련되어야 합니다. 반환되는 것은 전역 시간입니다.

var date = new Date();
document.write(date.getUTCHours());  //现在北京时间是2012-12-19 23:44,但是返回的是15,也就是全球时间中的小时数。


15. Date.getUTCMilliserconds() // Date 객체의 밀리초 수를 반환합니다(글로벌 시간)

var date = new Date();
document.write(date.getMilliseconds());  //返回全球时间中的毫秒数


16. Date.getUTCMinutes() // Date 객체의 분 수를 반환합니다(글로벌 시간)

var date = new Date();
document.write(date.getMinutes());  //2012-12-19 23:49  返回49,注意是全球时间,其实全球时间应该就小时不同而已吧。


17. Date.getUTCMonth()  //Date 객체의 월 값을 반환합니다(글로벌 시간)

var date = new Date();
document.write(date.getMonth());  //2012-12-19  返回11,0(1月份)-11(12月份)  


18. Date.getUTCSeconds() // Date 객체의 초 값을 반환합니다.

var date = new Date();
document.write(date.getSeconds());  //返回秒数值 返回33


Nineteen, Date.getYear() // Date 객체의 연도 값에서 1900을 뺀 값을 반환합니다.

var date = new Date();
document.write(date.getYear());  //2012-12-19  返回112 (2012-1900)


Twenty, Date .now() 정적 메서드 // 1970년 1월 1일 자정부터 현재까지의 시간 간격을 밀리초 단위로 반환합니다.

document.write(Date.now());  //静态方法,返回当前时间与1970-01-01的时间间隔,毫秒单位。


Twenty 1. Date.parse() // 날짜 및 시간 문자열을 구문 분석하고 1970-01-01 자정과 주어진 날짜 사이의 밀리초 수를 반환합니다.

var date = "2012-12-19";
document.write(Date.parse(date));  //返回  1355875200000
var da = new Date(date);
document.write("<br/>" + da.getFullYear() + "-" + da.getMonth() + "-" + da.getDate());  //输出2012-11-19  //注意月份是从0-11


22. Date.setDate() //Date 객체에 날짜 값을 설정하고, 반환 값은 조정된 날짜의 밀리초 단위로 표현됩니다.

var date = new Date();
document.write(date.setDate(11));  //返回1355236647980    //设置为11,其实是12月,设置为3其实是4月
    var da = new Date(date);
document.write("<br/>" + da.getFullYear() + "-" + da.getMonth() + "-" + da.getDate()); //输出2012-11-11  //注意月份是从0-11,设置的时候要注意


23. Date.setFullYear() //Date 객체에 연도를 설정하고, 반환값은 조정된 날짜의 밀리초 단위로 표현됩니다.

var date = new Date();  今天是2012-12-20
document.write(date.setFullYear(1989)); //返回630167981030
var da = new Date(date);
document.write("<br/>" + da.getFullYear() + "-" + da.getMonth() + "-" + da.getDate()); //输出1989-11-20


Twenty-four, Date.setHours() //Date 객체에 이벤트 수를 설정하고 반환 값은 다음과 같습니다. 밀리초로 표현된 조정된 날짜입니다.

var date = new Date(); //现在是2012-12-52 22:52
document.write(date.setHours(5)); //返回1355954000882
var da = new Date(date);
document.write("<br/>" + da.getHours()); //输出05


25, Date.setMilliseconds() //날짜의 밀리초 수 설정

var date = new Date(); //现在是2012-12-20
document.write(date.setMilliseconds(22)); //返回1356015393022    注意最后两位,无论如何刷新都是22


26. Date.setMinutes() //날짜의 분 설정

var date = new Date(); //现在是2012-12-52 22:52
document.write(date.setMinutes(1)); //返回1356012067105
var da = new Date(date);
document.write("<br/>" + da.getMinutes()); //输出1


Twenty-seven, Date.setMonth() // 날짜의 개월수 설정

var date = new Date(); //现在是2012-12-20
document.write(date.setMonth(2)); //返回1332255597722
var da = new Date(date);
document.write("<br/>" + da.getMonth()); //输出2


Twenty -eight , Date.setSeconds() //날짜의 설명 구문 설정: date.setSeconds(초) Date.setSeconds(초,밀리스)

var date = new Date(); //现在是2012-12-20
document.write(date.setSeconds(3)); //返回1356015783872
var da = new Date(date);
document.write("<br/>" + da.getSeconds()); //输出3


29, Date.setTime()                              밀리초 단위로 시간 구문 설정: date.setTime(millieonds)

var date = new Date(); //现在是2012-12-20
document.write(date.setTime(1356015783872)); //返回1356015783872
var da = new Date(date);
document.write("<br/>" + da.getDate()); //输出20


Thirty, Date.setUTCDate() //Date 객체에 월에 해당하는 날짜 값을 설정합니다. 이는 일(글로벌 시간)입니다. 구문: date.setUTCDate(day-of-month)

var date = new Date(); //现在是2012-12-20
document.write(date.setUTCDate(12)); //返回1355324952003
var da = new Date(date);
document.write("<br/>" + da.getDate()); //输出12


三十一、Date.setUTCFullYear()     //设置一个Date对象中对应的年份,全球时间      语法:date.setUTCFullYear(year)         date.setUTCFullYear(year,month)         date.setUTCFullYear(year,month,day)

var date = new Date(); //现在是2012-12-20
document.write(date.setUTCFullYear(1999)); //返回945702713666
var da = new Date(date);
document.write("<br/>" + da.getFullYear()); //输出1999

   


三十二、Date.setUTCHours()      //设置一个Date对象中对应的小时数,(全球时间)      语法:date.setUTCHours(hours)          date.setUTCHours(hours,minutes)         date.setUTCHours(hours,minutes,seconds)         date.setUTCHours(hours,minutes,seconds,millis)

var date = new Date(); //现在是2012-12-20
document.write(date.setUTCHours(05)); //返回1355980581928
var da = new Date(date);
document.write("<br/>" + da.getUTCHours()); //输出5

   


三十三、Date.setUTCMilliseconds()  //设置一个Date对象中对应的毫秒数,(全球时间)     语法:date.setUTCMilliseconds(millis)

var date = new Date(); //现在是2012-12-20
document.write(date.setMilliseconds(05)); //返回1356016784005  注意此处无论如何刷新都是05结尾

   


三十四、Date.setUTCMinutes()    //设置一个Date对象的分钟、秒钟、以及毫秒值。     语法:date.setUTCMinutes(minutes)     date.setUTCMinutes(minutes,seconds)                date.setUTCMinutes(minutes,seconds,millis)

var date = new Date(); //现在是2012-12-20
document.write(date.setUTCMinutes(25)); //返回1356017146549
var da = new Date(date);
document.write("<br/>" + da.getUTCMinutes()); //输出5

   


三十五、Date.setUTCMonth()    //设置一个Date对象的月份值及日期值        语法:date.setUTCMonth(month)          date.setUTCMonth(month,day)

var date = new Date(); //现在是2012-12-20
document.write(date.setMonth(01)); //返回1329751527983
var da = new Date(date);
document.write("<br/>" + da.getUTCMonth()); //输出1

   


三十六、Date.setUTCSeconds()    //设置一个Date的秒钟及毫秒值      语法:date.setUTCSeconds(seconds)         date.setUTCSeconds(seconds,millis)

var date = new Date(); //现在是2012-12-20
document.write(date.setUTCSeconds(01)); //返回1356017281976
var da = new Date(date);
document.write("<br/>" + da.getUTCSeconds()); //输出1

   


三十七、Date.setYears()      //设置一个Date对象的年份值,如果给的参数在0-99之间,它将会加上1900以便把它当中1900-1999之间的年份处理。如果输入4位数                    则把它当成FullYear设置      语法:date.setYears(year)

var date = new Date(); //现在是2012-12-20
document.write(date.setYear(22)); //返回1356017281976
var da = new Date(date);
document.write("<br/>" + da.getFullYear()); //输出1922
var date = new Date(); //现在是2012-12-20
document.write(date.setYear(2011)); //返回1324395113386
var da = new Date(date);
document.write("<br/>" + da.getFullYear()); //输出2011

   


三十八、Date.toDateString()    //以字符串的形式返回一个Date的日期部分     语法:date.toDateString()

var date = new Date(); //现在是2012-12-20
document.write(date.toDateString("yyyy-MM-dd")); //返回Thu Dec 20 2012

   


三十九、Date.toTimeString()    //以字符串的形式返回一个Date的时间部分     语法:date.toTimeString()

var date = new Date(); //现在是2012-12-20
document.write(date.toTimeString("yyyy-MM-dd")); //返回23:38:33 GMT+0800

   


 四十、Date.toISOString()      //将一个Date对象转换为ISO-8601格式的字符串     语法;date.toISOString()  //返回的字符串格式为yyyy-mm-ddThh:mm:ssZ

var date = new Date(); //现在是2012-12-20
document.write(date.toISOString()); //返回2012-12-20T15:45:47.493Z

   


四十一、Date.toJSON      //JSON序列化一个对象     语法:date.toJSON(key)  //date的一个字符串表示形式,值为调用它的toISOString()方法的结果

var date = new Date(); //现在是2012-12-20
document.write(date.toJSON()); //返回2012-12-20T15:45:47.493Z

   


四十二、Date.toLocaleDateString()  //以本地格式的字符串返回一个Date的日期部分语法:date.toLolcaleDateString  //返回一个本地人可读的日期格式,日期部分

var date = new Date(); //现在是2012-12-20
document.write(date.toLocaleDateString()); //返回2012年12月20日

   


 四十三、Date.toLocaleString()    //将一个Date转化难为一个本地格式的字符串 语法:date.toLocaleString()

var date = new Date(); //现在是2012-12-22
document.write(date.toLocaleString()); //返回2012年12月22日 19:56:40

   


四十四、Date.toLocaleTimeString()    //将一个Date转化为本地的格式的时间部分

var date = new Date(); //现在是2012-12-22
document.write(date.toLocaleTimeString()); //返回19:57:23

   


四十五、Date.toString()          //将一个Date转换为一个字符串

var date = new Date(); //现在是2012-12-22
document.write(date.toString()); //返回Sat Dec 22 2012 19:59:17 GMT+0800

   


四十六、Date.toTimeString()       //以字符串的形式返回一个Date对象的时间部分

var date = new Date(); //现在是2012-12-22
document.write(date.toString()); //返回Sat Dec 22 2012 19:59:17 GMT+0800

   


四十七、Date.toUTCString()      //将一个Date对象转换为字符串(全球时间)

var date = new Date(); //现在是2012-12-22
document.write(date.toUTCString()); //返回Sat, 22 Dec 2012 12:00:42 GMT

   


四十八、Date.UTC()        //将一个Date对象转换毫秒的形式  静态方法语法:Date.UTC(year,month,day,hours,minutes,seconds,ms)

document.write(Date.UTC(2011, 11, 11, 11, 11, 11)); //返回1323601871000


四十九、Date.valueOf() 

//如果是一个Date对象,将一个Date对象转为毫秒的形式,否则不显示

var date = "";
document.write(date.valueOf()); //不是Date对象,不输出
var date1 = new Date();
document.write(date1.valueOf()); //输出1356180400916

   



성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.