0
{"segments":    
    [
        {
            "Group":0,
            "Carrier":"TK",
            "FlightNumber":"###",
            "ChangeOfPlane":false,
            "OptionalServicesIndicator":false,
            "FlownSegment":false,
            "ScheduleChange":false,
            "Origin":"LHR",
            "Destination":"IST",
            "DepartureTime":"2016-04-12T16:30:00.000+01:00",
            "ArrivalTime":"2016-04-12T22:20:00.000+03:00"
        }
        {
            "Group":0,
            "Carrier":"TK",
            "FlightNumber":"118",
            "ChangeOfPlane":false,
            "OptionalServicesIndicator":false,
            "FlownSegment":false,
            "ScheduleChange":false,
            "Origin":"IST",
            "Destination":"KHI",
            "DepartureTime":"2016-04-13T00:10:00.000+03:00"
        }
    ]
}

this is the structure what i want is this string "DepartureTime":"2016-04-12T16:30:00.000+01:00" into this format "21 Apr 04:40" can anyone help me in this i am new to angular js and i want to have a filter that can convert my string to my desire result. Many Thanks

3
  • Can you format your question so that it is more readable? Your question seems to actually be "given string x, how does one convert it to string y?" stackoverflow.com/help/formatting Commented Apr 11, 2016 at 14:50
  • You basically want to parse the date right? Commented Apr 11, 2016 at 14:55
  • yes i want this "2016-04-13T00:10:00.000+03:00" string to convert into "13 Apr 00:10" this using angular js filter so that when i call departuretime with the filter it can convert my string. Commented Apr 11, 2016 at 14:56

4 Answers 4

1

you need this in your scope

var s = "2016-04-13T00:10:00.000+03:00"; //DepartureTime
$scope.v = {
    Dt: s
}

and in your html with filter

Angular: {{v.Dt | date:'dd MMMM HH:mm'}} <br />

Updated: Parse your json, based on what you have written in comments

in your ctrl

var m = yourJson; 
$scope.items = m.segments;

in your html

<li ng-repeat="i in items">{{i.DepartureTime}}</li>

and now you can use the filter

Sign up to request clarification or add additional context in comments.

3 Comments

Best answer so far, but I think it could look more professional without that "bro" and focus more on the filter explanation (OP might already have his date in-scope ; or maybe he might need to convert it before display for some reason)
the web service returns me $scope.segments where in my view i use ng-repeat and use {{ d.DepartureTime}} so what you have done is exactly i want to do but the thing is how to access the array string like 'DepartureTime' and use that filter. there may be many arrays and each contain such string manythanks @EzequielGonzálezGarcía
@resume you need to make this var m = yourJson; $scope.items = m.segments; in your html <li ng-repeat="i in items">{{i.DepartureTime}}</li>
1

I advice that you use someone else's code for this like edrian said (moment.js really is amazing).

However, if you want to do this by yourself, in native javascript, you need to parse the date yourself. You can refer to Mozilla's developer Date page where you'll find information on how to work with the date.

I've built a small jsfiddle for you that does exactly what you want: https://jsfiddle.net/ruwqmd2u/

You can see that I'm parsing the whole code line by line after creating a date with your date, while calling a function to deal with the month name:

var myDate = new Date("2016-04-12T16:30:00.000+01:00");

// Parse the date
var day = myDate.getDay();
var month = getMonthName(myDate.getMonth());
var hours = myDate.getHours();
var mins = myDate.getMinutes();

// Get a month name from number
function getMonthName(number) {
    if (number===1) return "Jan";
    if (number===2) return "Feb";
    if (number===3) return "Mar";
    if (number===4) return "Apr";
    if (number===5) return "May";
    if (number===6) return "Jun";
    if (number===7) return "Jul";
    if (number===8) return "Aug";
    if (number===9) return "Sep";
    if (number===10) return "Oct";
    if (number===11) return "Nov";
    if (number===12) return "Dec";
}

2 Comments

thanks for this yes i have less time to do this right now i am going to use what edrain said i hope it works. but i definitely will try to work this out some other time @ted thanks for your help cheers
no problem. If edrain's solution fixed the problem for you, or mine, please note them as "accepted", so people know.
0

Best option to parse Dates objects is using moment.js

If you want to parse the date using a filter, you could install angular-moment module.

Then you can do something like this in your view (asuming that you assigned DepartureTime value to $scope.date):

<span>{{ date | amParse:'DD MMM HH:mm' }}</span>

Hope it helps1

1 Comment

can it be done with the split functions like we do in java script ?
0

Moment.js is a very good option for parsing dates. If you want to use it in your project, then the solution is very simple, like the below one:

moment(DepartureTime).format('DD MMM h:m');
e.g. moment('2016-04-12T16:30:00.000+01:00').format('DD MMM h:m');

You can choose its angular version. You may check out angular-momentjs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.