원하는 형식으로 문자열에서 날짜 형식을 지정하는 방법은 무엇입니까?Javascript 형식 날짜 문자열에서 날짜 형식
var dateString = "31/05/2014 12:53:51";
// function only accepts dates in the format "yyyy/mm/dd"
var date = new Date (dateString);
원하는 형식으로 문자열에서 날짜 형식을 지정하는 방법은 무엇입니까?Javascript 형식 날짜 문자열에서 날짜 형식
var dateString = "31/05/2014 12:53:51";
// function only accepts dates in the format "yyyy/mm/dd"
var date = new Date (dateString);
간단한 기능은 영업 이익 문자열을 구문 분석은 다음과 같습니다
// Return a Date object
// Expects string in format d/m/y h:m:s
// Separator is not important, order is
function parseDate(s) {
var b = s.split(/\D+/);
return new Date(b[2], --b[1], b[0], b[3], b[4], b[5]);
}
아니면 그냥 문자열을 포맷 할 경우 (I 숫자와 구분 기호 사이에 공백이 필요 없다고 추측 함) :
// Return a date in "yyyy/mm/dd" format
// Expects string in format d/m/y h:m:s
// Separator is not important, order is
function reformatDate(s) {
// Helper to format single digit numbers
function z(n) {return (n<10? '0' : '') + +n;}
var b = s.split(/\D+/);
return b[2] + '/' + z(b[1]) + '/' + z(b[0]);
}
도우미 기능은 i n 경우에는 한자리 숫자가 입력에 채워지지 않습니다. 그들이 항상 존재한다고 확신한다면 그것을 제거 할 수 있습니다.
가장 쉬운 방법은 Moment.js과 같은 타사 라이브러리를 사용하는 것입니다. 귀하의 예제의
코드 사용 moment.js :
var date = moment("31/05/2014 12:53:51", "DD/MM/YYYY HH:mm:ss").toDate();
먼저 문자열을 구성 부분으로 구문 분석하십시오. 그런 다음 [* Date constructor *] (http://ecma-international.org/ecma-262/5.1/#sec-15.9.3.1)에서 날짜를 만드십시오. 그런 다음 [* getFullYear *] (http://ecma-international.org/ecma-262/5.1/#sec-15.9.5.10), [* getMonth *] (http://ecma-international.org/)와 같은 메소드를 사용하십시오. ecma-262/5.1/# sec-15.9.5.12) 등 원하는 형식으로 구성 할 수 있습니다. – RobG