development

JavaScript에서 올해 가져 오기

big-blog 2020. 9. 28. 09:34
반응형

JavaScript에서 올해 가져 오기


JavaScript로 올해를 어떻게 구합니까?


크리에이트 new Date()객체와 전화를 getFullYear():

new Date().getFullYear()
// returns the current year

현재 연도를 항상 표시하는 바닥 글과 같은 몇 가지 기본 예제 컨텍스트를 제공하기 위해 허용 된 답변을 도용합니다.

<footer>
    &copy; <span id="year">2018</span>
</footer>

위의 HTML이로드 된 후 실행되는 다른 곳 :

<script>
    document.getElementById("year").innerHTML = new Date().getFullYear();
</script>

document.getElementById("year").innerHTML = new Date().getFullYear();
footer {
  text-align: center;
  font-family: sans-serif;
}
<footer>
    &copy; <span id="year">2018</span> by FooBar
</footer>


// Return today's date and time
var currentTime = new Date()

// returns the month (from 0 to 11)
var month = currentTime.getMonth() + 1

// returns the day of the month (from 1 to 31)
var day = currentTime.getDate()

// returns the year (four digits)
var year = currentTime.getFullYear()

// write output MM/dd/yyyy
document.write(month + "/" + day + "/" + year)

날짜를 얻는 또 다른 방법이 있습니다.

new Date().getDate()          // Get the day as a number (1-31)
new Date().getDay()           // Get the weekday as a number (0-6)
new Date().getFullYear()      // Get the four digit year (yyyy)
new Date().getHours()         // Get the hour (0-23)
new Date().getMilliseconds()  // Get the milliseconds (0-999)
new Date().getMinutes()       // Get the minutes (0-59)
new Date().getMonth()         // Get the month (0-11)
new Date().getSeconds()       // Get the seconds (0-59)
new Date().getTime()          // Get the time (milliseconds since January 1, 1970)

금년 에는 Date 클래스 에서 getFullYear ()를 사용할 수 있지만 요구 사항에 따라 사용할 수있는 함수가 많이 있으며 일부 함수는 다음과 같습니다.

var now = new Date()
console.log("Current Time is: " + now);

// getFullYear function will give current year 
var currentYear = now.getFullYear()
console.log("Current year is: " + currentYear);

// getYear will give you the years after 1990 i.e currentYear-1990
var year = now.getYear()
console.log("Current year is: " + year);

// getMonth gives the month value but months starts from 0
// add 1 to get actual month value 
var month = now.getMonth() + 1
console.log("Current month is: " + month);

// getDate gives the date value
var day = now.getDate()
console.log("Today's day is: " + day);


이것이 내 HTML 웹 페이지에 포함되고 출력되는 방법입니다.

<div class="container">
    <p class="text-center">Copyright &copy; 
        <script>
            var CurrentYear = new Date().getFullYear()
            document.write(CurrentYear)
        </script>
    </p>
</div>

HTML 페이지에 대한 출력은 다음과 같습니다.

저작권 © 2018


이와 같이 간단히 자바 스크립트를 사용할 수 있습니다. 그렇지 않으면 대규모 응용 프로그램에 도움이되는 momentJs 플러그인을 사용할 수 있습니다 .

new Date().getDate()          // Get the day as a number (1-31)
new Date().getDay()           // Get the weekday as a number (0-6)
new Date().getFullYear()      // Get the four digit year (yyyy)
new Date().getHours()         // Get the hour (0-23)
new Date().getMilliseconds()  // Get the milliseconds (0-999)
new Date().getMinutes()       // Get the minutes (0-59)
new Date().getMonth()         // Get the month (0-11)
new Date().getSeconds()       // Get the seconds (0-59)
new Date().getTime()          // Get the time (milliseconds since January 1, 1970)

function generate(type,element)
{
	var value = "";
	var date = new Date();
	switch (type) {
		case "Date":
			value = date.getDate();		// Get the day as a number (1-31)
			break;
		case "Day":
			value = date.getDay();		// Get the weekday as a number (0-6)
			break;
		case "FullYear":
			value = date.getFullYear();	// Get the four digit year (yyyy)
			break;
		case "Hours":
			value = date.getHours();	// Get the hour (0-23)
			break;
		case "Milliseconds":
			value = date.getMilliseconds();	// Get the milliseconds (0-999)
			break;
		case "Minutes":
			value = date.getMinutes();     // Get the minutes (0-59)
			break;
		case "Month":
			value = date.getMonth();	// Get the month (0-11)
			break;
		case "Seconds":
			value = date.getSeconds();	// Get the seconds (0-59)
			break;
		case "Time":
			value = date.getTime();		// Get the time (milliseconds since January 1, 1970)
			break;
	}

	$(element).siblings('span').text(value);
}
li{
  list-style-type: none;
  padding: 5px;
}

button{
  width: 150px;
}

span{
  margin-left: 100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<ul>
	<li>
		<button type="button" onclick="generate('Date',this)">Get Date</button>
		<span></span>
	</li>
	<li>
		<button type="button" onclick="generate('Day',this)">Get Day</button>
		<span></span>
	</li>
	<li>
		<button type="button" onclick="generate('FullYear',this)">Get Full Year</button>
		<span></span>
	</li>
	<li>
		<button type="button" onclick="generate('Hours',this)">Get Hours</button>
		<span></span>
	</li>
	<li>
		<button type="button" onclick="generate('Milliseconds',this)">Get Milliseconds</button>
		<span></span>
	</li>

	<li>
		<button type="button" onclick="generate('Minutes',this)">Get Minutes</button>
		<span></span>
	</li>
	<li>
		<button type="button" onclick="generate('Month',this)">Get Month</button>
		<span></span>
	</li>
	<li>
		<button type="button" onclick="generate('Seconds',this)">Get Seconds</button>
		<span></span>
	</li>
	<li>
		<button type="button" onclick="generate('Time',this)">Get Time</button>
		<span></span>
	</li>
</ul>


Take this example, you can place it wherever you want to show it without referring to script in the footer or somewhere else like other answers

<script>new Date().getFullYear()>document.write(new Date().getFullYear());</script>

Copyright note on the footer as an example

Copyright 2010 - <script>new Date().getFullYear()>document.write(new Date().getFullYear());</script>

참고URL : https://stackoverflow.com/questions/6002254/get-the-current-year-in-javascript

반응형