admin管理员组文章数量:1406937
I'm getting a date from an API as
2017-04-20T07:00:00Z
How could I format it into the following?
20.04.2017
I'm using React to display the date:
<div>{props.data.day}</div>
I tried for example toISOString().slice(0, 10); but couldn't get that to work properly. I also tried looking at other answers, but couldn't find a solution yet.
I'm getting a date from an API as
2017-04-20T07:00:00Z
How could I format it into the following?
20.04.2017
I'm using React to display the date:
<div>{props.data.day}</div>
I tried for example toISOString().slice(0, 10); but couldn't get that to work properly. I also tried looking at other answers, but couldn't find a solution yet.
Share Improve this question edited Feb 6, 2017 at 13:15 Ashish Bahl 1,5021 gold badge18 silver badges28 bronze badges asked Feb 6, 2017 at 12:26 user45user45 532 silver badges6 bronze badges 3- Probably duplicate of Changing the format of a date string. – RobG Commented Feb 6, 2017 at 12:29
- 1 also: have a look at moment.js – nozzleman Commented Feb 6, 2017 at 12:29
- Do you want to use UTC date or convert to local date? Hawaii is UTC-1000 so for them 2017-04-20T07:00:00Z is 19 April 2017 at 9pm (i.e. 2017-04-19T021:00:00UTC-1000). – RobG Commented Feb 6, 2017 at 12:32
5 Answers
Reset to default 2The following should work without any third party libraries:
console.log(convertDate(new Date("2017-04-20T07:00:00Z")));
function convertDate(date) {
var day = date.getDate();
day = day < 10 ? "0" + day : day;
var month = date.getMonth() + 1;
month = month < 10 ? "0" + month : month;
var year = date.getFullYear();
return day + "." + month + "." + year;
}
This gives:
20.04.2017
You would need to add this to your ponent as a function and place the return value as the content of your div
.
Please note, as noted in other answers, you are better off using a third party library that can deal with timezones and other time-based problems without you having to think about it.
If you store the date as string you can convert it this way:
var i ='2017-04-20T07:00:00Z';
var date = i.split('T');
console.log(date[0].split('-').reverse().join('.'))
this gives:
20.04.2017
You could use MomentJS to parse and format the date-string. Choose a library that already does this for you. You should worry about your business logic. Use mature; widely-used libraries to do the heavy lifting for you.
console.log(moment('2017-04-20T07:00:00Z', moment.ISO_8601).format('DD.MM.YYYY'));
.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.17.1/moment.min.js"></script>
Part of software development is code-reuse. Since you are not developing a date library, you should not worry about creating one.
You can parse the result with Regex.
function convertDate(date){
var parsedDate = date.match(/^([\d]{4})-([\d]{2})-([\d]{2})/)
return parsedDate[2] + '.' + parsedDate[1] + '.' + parsedDate[0]
}
convertDate('2017-04-20T07:00:00Z')
You can extract the day, month and full year to then use as you wish!
var oldDate = new Date('2017-04-20T07:00:00Z');
var d = oldDate.getDay().toString();
var m = oldDate.getMonth().toString();
var y = oldDate.getFullYear().toString();
if(d.length < 2){
d = '0' + d;
}
if(m.length < 2){
m = '0' + m;
}
var myDate = d + '/' + m + '/' + y;
window.alert(myDate);
本文标签: How to convert date into another format in JavaScriptStack Overflow
版权声明:本文标题:How to convert date into another format in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744962840a2634760.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论