반응형
moment.js로 날짜 형식 지정
이 형식의 문자열이 있습니다.
var testDate = "Fri Apr 12 2013 19:08:55 GMT-0500 (CDT)"
moment.js를 사용 하여이 형식 mm/dd/yyyy : 04/12/2013
으로 표시 하고 싶습니다 .
이 방법을 사용하여 시도했지만
moment(testDate,'mm/dd/yyyy');
어떤 오류와 말합니다 there is no such method called replace
? 내가 잘못 접근하고 있습니까?
편집하다:
또한 meteor.js 용으로 패키지 된 moment.js의 사전 패키지 버전을 사용하고 있다고 언급해야합니다.
Object [object Date] has no method 'replace' : The Exact error from the console
스택 추적 :
at makeDateFromStringAndFormat (http://127.0.0.1:3000/packages/moment/lib/moment/moment.js?b4e3ac4a3d0794023a4410e7941c3e179398b5b0:539:29)
at moment (http://127.0.0.1:3000/packages/moment/lib/moment/moment.js?b4e3ac4a3d0794023a4410e7941c3e179398b5b0:652:24)
at populateProfileForEdit (http://127.0.0.1:3000/client/views/home/administration/directory/profiles/profiles.js?acfff908a6a099f37312f62892a22b40f82e5e0f:147:25)
at Object.Template.profile_personal.rendered (http://127.0.0.1:3000/client/views/home/administration/directory/profiles/profiles.js?acfff908a6a099f37312f62892a22b40f82e5e0f:130:13)
at Spark.createLandmark.rendered (http://127.0.0.1:3000/packages/templating/deftemplate.js?b622653d121262e50a80be772bf5b1e55ab33881:126:42)
at http://127.0.0.1:3000/packages/spark/spark.js?45c746f38023ceb80745f4b4280457e15f058bbc:384:32
at Array.forEach (native)
at Function._.each._.forEach (http://127.0.0.1:3000/packages/underscore/underscore.js?867d3653d53e9c7a171483edbcad9670e12288c7:79:11)
at http://127.0.0.1:3000/packages/spark/spark.js?45c746f38023ceb80745f4b4280457e15f058bbc:382:7
at _.extend.flush (http://127.0.0.1:3000/packages/deps/deps.js?9642a93ae1f8ffa8eb1c2475b198c764f183d693:231:11)
에 제 2 인수는 moment()
A는 구문 분석 형식 보다는 디스플레이 형식입니다.
이를 위해 다음과 같은 .format()
방법 을 원합니다 .
moment(testDate).format('MM/DD/YYYY');
또한 사건은 중요합니다. 월, 일 및 년의 경우 형식은 대문자 여야합니다.
moment.js를 포함시키고 아래 코드를 사용하여 날짜 형식을 지정할 수 있습니다
var formatDate= 1399919400000;
var responseDate = moment(formatDate).format('DD/MM/YYYY');
내 출력은 "13/05/2014"입니다
출력 날짜를 확인하려면을 사용하십시오 format
. 두 번째 순간 논쟁은 파싱을위한 것이지만, 생략 testDate
하면 사용 중단 경고가 발생합니다.
지원 중단 경고 : 제공된 값이 인식 된 RFC2822 또는 ISO 형식이 아닙니다 ...
var testDate= "Fri Apr 12 2013 19:08:55 GMT-0500 (CDT)"
let s= moment(testDate).format('MM/DD/YYYY');
msg.innerText= s;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<div id="msg"></div>
이 경고를 생략하려면 구문 분석 형식을 제공해야합니다
var testDate= "Fri Apr 12 2013 19:08:55 GMT-0500 (CDT)"
let s= moment(testDate, 'ddd MMM D YYYY HH:mm:ss ZZ').format('MM/DD/YYYY');
console.log(s);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
moment().format(); // "2019-08-12T17:52:17-05:00" (ISO 8601, no fractional seconds)
moment().format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Monday, August 12th 2019, 5:52:00 pm"
moment().format("ddd, hA"); // "Mon, 5PM"
참고 URL : https://stackoverflow.com/questions/15993913/format-date-with-moment-js
반응형
'Programing' 카테고리의 다른 글
Hibernate 및 MySQL을 사용한 생성 타임 스탬프 및 마지막 업데이트 타임 스탬프 (0) | 2020.04.18 |
---|---|
작업 표시 줄의 텍스트를 변경하는 방법 (0) | 2020.04.18 |
PyPlot의 역 Y 축 (0) | 2020.04.18 |
자바 : 유닉스 타임 스탬프 날짜 (0) | 2020.04.18 |
Web Api의 요청과 일치하는 여러 조치가 발견되었습니다. (0) | 2020.04.18 |