다음은 시간을 마이크로 초 (예 : 4565212462)로 반환합니다.
alert( $.now() );
(시 : 분 : 초) 와 같이 사람이 읽을 수있는 시간 형식으로 변환하려면 어떻게합니까 ?
답변
다음과 같이 시도해보십시오.
new Date($.now());
또한 Javascript를 사용하면 다음과 같이 할 수 있습니다.
var dt = new Date();
var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
document.write(time);
답변
모든 “숫자”를 수동으로 가져와야합니다.
이처럼 :
var currentdate = new Date();
var datetime = "Now: " + currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " @ "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
document.write(datetime);
답변
예를 들어 다음 의 변환 getterDate
중 하나를 사용하여 객체를 문자열로 변환합니다 .Date.prototype
var d = new Date();
d+''; // "Sun Dec 08 2013 18:55:38 GMT+0100"
d.toDateString(); // "Sun Dec 08 2013"
d.toISOString(); // "2013-12-08T17:55:38.130Z"
d.toLocaleDateString() // "8/12/2013" on my system
d.toLocaleString() // "8/12/2013 18.55.38" on my system
d.toUTCString() // "Sun, 08 Dec 2013 17:55:38 GMT"
또는 더 커스터마이징 하려면 Date.prototype
getter 메소드 목록을 참조하십시오 .
답변
이를 위해 jQuery를 사용할 필요가 없습니다!
Date.now()
와 $.now()
같은 값을 반환 :
Date.now(); // 1421715573651
$.now(); // 1421715573651
new Date(Date.now()) // Mon Jan 19 2015 20:02:55 GMT-0500 (Eastern Standard Time)
new Date($.now()); // Mon Jan 19 2015 20:02:55 GMT-0500 (Eastern Standard Time)
.. 시간을 hh-mm-ss로 형식화하려는 경우 :
var now = new Date(Date.now());
var formatted = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
// 20:10:58
답변
.clock {
width: 260px;
margin: 0 auto;
padding: 30px;
color: #FFF;background:#333;
}
.clock ul {
width: 250px;
margin: 0 auto;
padding: 0;
list-style: none;
text-align: center
}
.clock ul li {
display: inline;
font-size: 3em;
text-align: center;
font-family: "Arial", Helvetica, sans-serif;
text-shadow: 0 2px 5px #55c6ff, 0 3px 6px #55c6ff, 0 4px 7px #55c6ff
}
#Date {
font-family: 'Arial', Helvetica, sans-serif;
font-size: 26px;
text-align: center;
text-shadow: 0 2px 5px #55c6ff, 0 3px 6px #55c6ff;
padding-bottom: 40px;
}
#point {
position: relative;
-moz-animation: mymove 1s ease infinite;
-webkit-animation: mymove 1s ease infinite;
padding-left: 10px;
padding-right: 10px
}
/* Animasi Detik Kedap - Kedip */
@-webkit-keyframes mymove
{
0% {opacity:1.0; text-shadow:0 0 20px #00c6ff;}
50% {opacity:0; text-shadow:none; }
100% {opacity:1.0; text-shadow:0 0 20px #00c6ff; }
}
@-moz-keyframes mymove
{
0% {opacity:1.0; text-shadow:0 0 20px #00c6ff;}
50% {opacity:0; text-shadow:none; }
100% {opacity:1.0; text-shadow:0 0 20px #00c6ff; }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// Making 2 variable month and day
var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
var dayNames= ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
// make single object
var newDate = new Date();
// make current time
newDate.setDate(newDate.getDate());
// setting date and time
$('#Date').html(dayNames[newDate.getDay()] + " " + newDate.getDate() + ' ' + monthNames[newDate.getMonth()] + ' ' + newDate.getFullYear());
setInterval( function() {
// Create a newDate() object and extract the seconds of the current time on the visitor's
var seconds = new Date().getSeconds();
// Add a leading zero to seconds value
$("#sec").html(( seconds < 10 ? "0" : "" ) + seconds);
},1000);
setInterval( function() {
// Create a newDate() object and extract the minutes of the current time on the visitor's
var minutes = new Date().getMinutes();
// Add a leading zero to the minutes value
$("#min").html(( minutes < 10 ? "0" : "" ) + minutes);
},1000);
setInterval( function() {
// Create a newDate() object and extract the hours of the current time on the visitor's
var hours = new Date().getHours();
// Add a leading zero to the hours value
$("#hours").html(( hours < 10 ? "0" : "" ) + hours);
}, 1000);
});
</script>
<div class="clock">
<div id="Date"></div>
<ul>
<li id="hours"></li>
<li id="point">:</li>
<li id="min"></li>
<li id="point">:</li>
<li id="sec"></li>
</ul>
</div>
답변
jQuery.now()
반환 : 숫자
설명 : 현재 시간을 나타내는 숫자를 반환합니다.
이 메소드는 인수를 허용하지 않습니다.
이 $.now()
메서드는 expression에서 반환 한 숫자의 약어입니다 (new Date).getTime()
.
http://api.jquery.com/jQuery.now/
자바 스크립트를 사용하는 것은 간단합니다 :
var d = new Date();
var time = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
console.log(time);
답변
jQuery의 $ .now ()는 내부 Javascript 함수 인 new Date (). getTime ()의 별칭입니다.
http://api.jquery.com/jquery.now/
이것은 1970 년 이후 경과 한 시간 (초)을 반환하며 원에 따라 일반적으로 Unix Time, Epoch 또는 Timestamp라고 칭합니다. 간단한 수학을 사용하여 날짜 / 시간의 차이를 계산하는 데 매우 유용 할 수 있습니다. . TimeZone 정보가 없으며 항상 UTC입니다.
http://en.wikipedia.org/wiki/Unix_time
이 별명 외에 jQuery를 사용할 필요가 없으며 날짜 / 시간 조작에 도움이되지 않습니다.
텍스트로 시간을 표현하는 빠르고 더러운 방법을 찾고 있다면 Javascript Date 객체에는 ISO 형식의 Date Time을 반환하는 “toString”프로토 타입이 있습니다.
new Date().toString();
//returns "Thu Apr 30 2015 14:37:36 GMT+0100 (BST)"
그래도 형식을 사용자 정의하고 싶을 것입니다. Date 객체에는 관련 세부 정보를 가져 와서 고유 한 문자열 표현을 만들 수있는 기능이 있습니다.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
var d = new Date(); //without params it defaults to "now"
var t = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
//Will return 14:37:36
그러나 jQuery 솔루션을 요청한 경우 이전 브라우저로 작업 중일 수 있습니다. 보다 구체적인 작업, 특히 문자열을 Date 객체로 해석 (API 응답에 유용)하려는 경우 Moment.js를 참조하십시오.
이렇게하면 브라우저 간 호환성이 보장되고 많은 문자열을 함께 연결하지 않고도 서식이 훨씬 향상됩니다! 예를 들면 다음과 같습니다.
moment().format('hh:mm:ss');
//Will return 14:37:36