1

This is input format: yyyy:MM:dd'T'HH:mm:ss'Z' (Coming as a string from json service)

Required output format: dd-mmm-yyyy

I have tried with {{txnDate | date:'dd-mm-yyyy'}} but it is not working..

3
  • I´ve never seen dates being returned like this from the server, please confirm to us that format. If you have access to the service, you should preferably fix that format, those : separating the date values (year, month and date) are really awkward. Commented Feb 19, 2015 at 13:57
  • If the format is not the issue, I see you are aiming to show the month´s name, if that´s the issue you should use this format: {{txnDate | date:'dd-MMM-yyyy'}} Commented Feb 19, 2015 at 13:59
  • ya..The format in the service is : @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy:MM:dd'T'HH:mm:ss'Z'", timezone = "UTC") Commented Feb 20, 2015 at 4:35

1 Answer 1

1

What is the format you are following for your date?

A quick var a = new Date(); a.toISOString(); in console will give you something like "2015-02-19T13:30:13.347Z". The formatted string you are receiving is not following any standard and I am afraid parsing it to date will result in Invalid Date in most of the browsers.

So you can either

  1. Get your Date in proper format.

  2. Make the best use of whatever is available. You can use split to break your string into individual components.

Something like:

var a = "yyyy:MM:dd'T'HH:mm:ss'Z'" //Replace with actual string

b=a.split(':') will result in ["yyyy", "MM", "dd'T'HH", "mm", "ss'Z'"] giving you year and months in b[0] and b[1].

For date, you can use b[2].substring(0,2) to give you dd.

You have all date components(apart from time components, which you don't need anyway) as string.

Either use them directly(as a string) or make a date object using these components(since you want month in MMM format).

$scope.txnDate = new Date(b[0]+'/'+b[1]+'/'+b[2].substring(0,2));

I am sure there are more ways to optimize this. Comment if this doesn't work for you, will try to elaborate more.

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

1 Comment

Hi aniket, I am returning return b[2].substring(0,2) +'/'+b[1] + '/' +b[0]; And its the output that I wanted.. :) Thanks ..

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.