일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
- WebView
- C#
- not working
- html5
- C# IO
- android 효과음
- FCM
- 안드로이드 gcm
- php 취약점
- curl
- mysql
- Android
- PHP
- roundcube
- chart.js
- php 시큐어코딩
- 우분투
- javascript
- soundpool
- xe
- 안드로이드 푸시
- Mail Server
- 안드로이드 푸쉬
- dovecot
- 자동 생성
- 폼메일
- 자바스크립트
- 안드로이드
- 설치
- UML
- Today
- Total
그러냐
날짜 및 시간 계산(JavaScript) 본문
var dt = new Date(); // Display the month, day, and year. getMonth() returns a 0-based number. var month = dt.getMonth()+1; var day = dt.getDate(); var year = dt.getFullYear(); document.write(month + '-' + day + '-' + year); // Output: current month, day, year
var dt = new Date('8/24/2009'); document.write(dt); // Output: Mon Aug 24 00:00:00 PDT 2009
중요 |
---|
|
var dt = new Date('2010-06-09T15:20:00Z'); document.write(dt); document.write("<br />"); document.write(dt.toISOString()); // Output: // Wed Jun 09 2010 08:20:00 GMT-0700 (Pacific Daylight Time) // 2010-06-09T15:20:00.000Z
var dtA = new Date('8/24/2009 14:52:10'); // The parameters are year, month, day, hours, minutes, seconds. var dtB = new Date(2009, 7, 24, 14, 52, 10); document.write(dtA); document.write("<br/>"); document.write(dtB); // Output: // Mon Aug 24 14:52:10 PDT 2009 // Mon Aug 24 14:52:10 PDT 2009
var myDate = new Date("1/1/1990"); var dayOfMonth = myDate.getDate(); myDate.setDate(dayOfMonth - 1); document.write(myDate); // Output: Sun Dec 31 00:00:00 PST 1989
팁 |
---|
|
var myDate = new Date("1/1/1990") myDate.setMonth(myDate.getMonth() + 1); myDate.setDate (myDate.getDate() - 1); document.write(myDate); // Output: Wed Jan 31 00:00:00 PST 1990
var myDate = new Date(); myDate.setHours(0, 0, 0, 0); myDate.setYear(2013); // Determine November 1. myDate.setDate(1); myDate.setMonth(10); // Find Thursday. var thursday = 4; while(myDate.getDay() != thursday) { myDate.setDate(myDate.getDate() + 1); } // Add 3 weeks. myDate.setDate(myDate.getDate() + 21); document.write(myDate); // Output: Thu Nov 28 00:00:00 <time zone> 2013
var startTime = new Date('1/1/1990'); var startMsec = startTime.getMilliseconds(); startTime.setTime(5000000); var elapsed = (startTime.getTime() - startMsec) / 1000; document.write(elapsed); // Output: 5000
// Set the unit values in milliseconds. var msecPerMinute = 1000 * 60; var msecPerHour = msecPerMinute * 60; var msecPerDay = msecPerHour * 24; // Set a date and get the milliseconds var date = new Date('6/15/1990'); var dateMsec = date.getTime(); // Set the date to January 1, at midnight, of the specified year. date.setMonth(0); date.setDate(1); date.setHours(0, 0, 0, 0); // Get the difference in milliseconds. var interval = dateMsec - date.getTime(); // Calculate how many days the interval contains. Subtract that // many days from the interval to determine the remainder. var days = Math.floor(interval / msecPerDay ); interval = interval - (days * msecPerDay ); // Calculate the hours, minutes, and seconds. var hours = Math.floor(interval / msecPerHour ); interval = interval - (hours * msecPerHour ); var minutes = Math.floor(interval / msecPerMinute ); interval = interval - (minutes * msecPerMinute ); var seconds = Math.floor(interval / 1000 ); // Display the result. document.write(days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds."); //Output: 164 days, 23 hours, 0 minutes, 0 seconds.
var birthday = new Date("8/1/1985"); var today = new Date(); var years = today.getFullYear() - birthday.getFullYear(); // Reset birthday to the current year. birthday.setFullYear(today.getFullYear()); // If the user's birthday has not occurred yet this year, subtract 1. if (today < birthday) { years--; } document.write("You are " + years + " years old."); // Output: You are <number of years> years old.
// Get the current date at midnight. var now = new Date(); var todayAtMidn = new Date(now.getFullYear(), now.getMonth(), now.getDate()); // Set specificDate to a specified date at midnight. var specificDate = new Date("9/21/2009"); // Compare the two dates by comparing the millisecond // representations. if (todayAtMidn.getTime() == specificDate.getTime()) { document.write("Same"); } else { document.write("Different"); } //Output: Different
// Get the current date at midnight. var now = new Date(); var todayAtMidn = new Date(now.getFullYear(), now.getMonth(), now.getDate()); // Set start/end dates to a specified date (ISO format). var startDate = new Date("2009-06-09T15:20:00Z"); var endDate = new Date("2011-06-09T15:20:00Z"); // Compare the two dates by comparing the millisecond // representations. if (todayAtMidn.getTime() > startDate.getTime() && todayAtMidn.getTime() < endDate.getTime()) { document.write("Specified date is within this range."); } else { document.write("Specified date is not in this range."); } // Output: Specified date is not in this range.
'javascript' 카테고리의 다른 글
[javascript]자바스크립트 난독화 시키기 (0) | 2016.11.23 |
---|---|
특정 문자 모두 바꾸기 (replaceAll) 쉽게 사용하기 (0) | 2016.10.31 |
Javascript에서 String을 Number타입으로 바꾸기 (0) | 2016.09.21 |
datepicker 시작일과 종료일 설정시 사용하기 좋은 팁 (0) | 2016.08.03 |
슬라이더 rangeSlider : JavaScript polyfill for HTML5 Range Slider Element (0) | 2016.04.20 |