admin管理员组文章数量:1335590
I am getting date from a API which is in string format, I converted that string to date but unable to change it's format. Currently I got string like this -> '20/11/2016' I want to convert this & should look like this -> 11/20/2016 & then only I can display this in html page using html 5 date input type
<input type='date' ng-model='myDate'>
myApp.controller('myCtrl',['$scope','$filter'],
function ($scope,$filter)
{
//consider this string es from api...
var stringDate='20/11/2016';
var convertedDate=new Date(stringDate);
$scope.myDate=$filter('date')(convertedDate,'MM/dd/yyyy');
});
I am getting date from a API which is in string format, I converted that string to date but unable to change it's format. Currently I got string like this -> '20/11/2016' I want to convert this & should look like this -> 11/20/2016 & then only I can display this in html page using html 5 date input type
<input type='date' ng-model='myDate'>
myApp.controller('myCtrl',['$scope','$filter'],
function ($scope,$filter)
{
//consider this string es from api...
var stringDate='20/11/2016';
var convertedDate=new Date(stringDate);
$scope.myDate=$filter('date')(convertedDate,'MM/dd/yyyy');
});
Share
Improve this question
asked Oct 25, 2016 at 10:53
MeVimalkumarMeVimalkumar
3,3612 gold badges18 silver badges27 bronze badges
5
- 1 Can you use momentjs ? – Zumry Mohamed Commented Oct 25, 2016 at 10:55
- I haven't yet, but I'll try right now – MeVimalkumar Commented Oct 25, 2016 at 10:55
- 1 rather than to use moment js , this filter can resolve this problem simply. – Jigar7521 Commented Oct 25, 2016 at 10:58
- in this case $filter didn't works. if I console it displayed Invalid Date @Jigar7521 – MeVimalkumar Commented Oct 25, 2016 at 11:13
- 1 @Jigar7521 moment js works for me and it the best solution. thank you, man... – MeVimalkumar Commented Sep 5, 2017 at 5:03
6 Answers
Reset to default 3var stringDate1=this.dStartDate;
var splitDate1 = stringDate1.split('-');
var year1 = splitDate1[0];
var month1 = splitDate1[1];
var day1 = splitDate1[2];
this.dStartDate = month1+"-"+day1+"-"+year1;
console.log("date"+this.dStartDate);
remove 'new' so
var stringDate='20/11/2016';
var convertedDate=new Date(stringDate);
should be
var stringDate='20/11/2016';
var convertedDate=Date(stringDate);
You can split and then pass to date object. After that using Date functions reform the date.
var dateArray = '31/12/2016'.split('/');
var convertedDate = new Date(dateArray[1]+'-'+dateArray[0]+' '+dateArray[2]);
Using momentjs
var convertedDate = moment('20/11/2016', 'dd/MM/yyyy');
Using vanilla javascript
var dateArray = '20/11/2016'.split('/');
var convertedDate = new Date(dateArray[1]+'-'+dateArray[0]+'-'+dateArray[2]);
Probably this is not the best implementation, but it works!
First of all you can not give stringDate
directly as input to Date object as it is not a valid Date string.
Revised code:
var stringDate='20/11/2016';
var splitDate = stringDate.split('/');
var convertedDate=new Date(splitDate[2], splitDate[1]-1, splitDate[0]);
console.log(convertedDate.toLocaleDateString("en-US"));
$scope.myDate = convertedDate.toLocaleDateString("en-US");
Here using en-US locale returns 11/20/2016 format. You can find more about toLocale conversion here.
Here you go:
var stringDate='20/11/2016';
$scope.myDate=$filter('date')(stringDate, 'MM/dd/yyyy');
本文标签:
版权声明:本文标题:javascript - How to change format of a date from ddmmyyyy to MMddyyyy where date is already converted from string? - Stack Overf 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742382926a2464454.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论