날짜를 타임 스탬프로 변환하는 방법?
날짜를 타임 스탬프로 변환하고 싶습니다 26-02-2012
. 입력은 입니다. 나는 사용했다
new Date(myDate).getTime();
NaN이라고합니다. 이것을 변환하는 방법을 아는 사람이 있습니까?
var myDate="26-02-2012";
myDate=myDate.split("-");
var newDate=myDate[1]+","+myDate[0]+","+myDate[2];
alert(new Date(newDate).getTime()); //will alert 1330192800000
최신 정보:
var myDate="26-02-2012";
myDate=myDate.split("-");
var newDate=myDate[1]+"/"+myDate[0]+"/"+myDate[2];
alert(new Date(newDate).getTime()); //will alert 1330210800000
데모 (Chrome, FF, Opera, IE 및 Safari에서 테스트)
이 함수를 사용해보십시오. Date.parse () 메서드를 사용하며 사용자 지정 논리가 필요하지 않습니다.
function toTimestamp(strDate){
var datum = Date.parse(strDate);
return datum/1000;
}
alert(toTimestamp('02/13/2009 23:31:30'));
var dtstr = "26-02-2012";
new Date(dtstr.split("-").reverse().join("-")).getTime();
이 리팩토링 된 코드는 그것을 할 것입니다
let toTimestamp = strDate => Date.parse(strDate)
이것은 ie8-를 제외한 모든 최신 브라우저에서 작동합니다.
날짜 숫자를 바꾸고 다음 -
으로 변경 하면됩니다 ,
.
new Date(2012,01,26).getTime(); // 02 becomes 01 because getMonth() method returns the month (from 0 to 11)
귀하의 경우 :
var myDate="26-02-2012";
myDate=myDate.split("-");
new Date(parseInt(myDate[2], 10), parseInt(myDate[1], 10) - 1 , parseInt(myDate[0]), 10).getTime();
PS UK 로켈은 여기서 중요하지 않습니다.
function getTimeStamp() {
var now = new Date();
return ((now.getMonth() + 1) + '/' + (now.getDate()) + '/' + now.getFullYear() + " " + now.getHours() + ':'
+ ((now.getMinutes() < 10) ? ("0" + now.getMinutes()) : (now.getMinutes())) + ':' + ((now.getSeconds() < 10) ? ("0" + now
.getSeconds()) : (now.getSeconds())));
}
여기에는 두 가지 문제가 있습니다. 먼저, 날짜 인스턴스에서만 getTime을 호출 할 수 있습니다. 새 날짜를 괄호로 묶거나 변수에 할당해야합니다.
둘째, 적절한 형식의 문자열을 전달해야합니다.
작업 예 :
(new Date("2012-02-26")).getTime();
문자열이 Date
객체가 처리하도록 지정된 형식이 아닙니다 . 직접 구문 분석 하거나 MomentJS 와 같은 날짜 구문 분석 라이브러리 또는 DateJS 와 같은 이전 구문 분석 라이브러리를 사용 하거나 구문 분석을 요청 하기 전에 올바른 형식으로 마사지하십시오 (예 :) .2012-02-29
Date
당신이 얻는 이유 NaN
: new Date(...)
유효하지 않은 문자열을 처리 하도록 요청 Date
하면 유효하지 않은 날짜로 설정된 객체를 new Date("29-02-2012").toString()
반환 합니다 ( returns "Invalid date"
). getTime()
이 상태에서 날짜 객체를 호출 하면을 반환합니다 NaN
.
/**
* Date to timestamp
* @param string template
* @param string date
* @return string
* @example datetotime("d-m-Y", "26-02-2012") return 1330207200000
*/
function datetotime(template, date){
date = date.split( template[1] );
template = template.split( template[1] );
date = date[ template.indexOf('m') ]
+ "/" + date[ template.indexOf('d') ]
+ "/" + date[ template.indexOf('Y') ];
return (new Date(date).getTime());
}
(ISO) 날짜를 Unix 타임 스탬프 로 변환하기 위해 필자는 필요한 시간보다 3 자 더 긴 타임 스탬프로 끝나서 올해 약 50k가되었습니다 ...
I had to devide it by 1000: new Date('2012-02-26').getTime() / 1000
For those who wants to have readable timestamp in format of, yyyymmddHHMMSS
> (new Date()).toISOString().replace(/[^\d]/g,'') // "20190220044724404"
> (new Date()).toISOString().replace(/[^\d]/g,'').slice(0, -3) // "20190220044724"
> (new Date()).toISOString().replace(/[^\d]/g,'').slice(0, -9) // "20190220"
Usage example: a backup file extension. /my/path/my.file.js.20190220
Answers have been provided by other developers but in my own way, you can do this on the fly without creating any user defined function as follows:
var timestamp = Date.parse("26-02-2012".split('-').reverse().join('-'));
alert(timestamp); // returns 1330214400000
incase you came here looking for current timestamp
var date = new Date();
var timestamp = date.getTime();
TLDR:
new Date().getTime();
참고URL : https://stackoverflow.com/questions/9873197/how-to-convert-date-to-timestamp
'Programing' 카테고리의 다른 글
iOS 5.1 SDK의 iPad 시뮬레이터에 "홈"버튼이없는 이유는 무엇입니까? (0) | 2020.06.23 |
---|---|
HttpClient를 사용하여 Https 호출 (0) | 2020.06.23 |
JavaScript를 사용하여 선택한 HTML 옵션을 어떻게 변경합니까? (0) | 2020.06.22 |
grep -f와 동등한 PowerShell (0) | 2020.06.22 |
IE 8 : 배경 크기 수정 (0) | 2020.06.22 |