0

My requirement is something different. I want to get the date format, not to format the date. Means I have a date string and now I want to get the date format of that date and apply it to the another date as a format.

Let me explain in brief with example:

var dateStr = "2015-06-06T12:00:00Z";
var d = new Date(dateStr);

here my date format is yyyy-MM-ddTHH:mm:ssZ you can see in dateStr object.

Now i will create another date and want to apply the same date-format to this new date.

var formatStr = "yyyy-MM-dd'T'HH:mm:ss'Z'"; // want to get this from above date, not hard coded like this.
var newDate = $filter('date')(d, formatStr);

here you can see that i have hard coded the format string, which i don't want to do. Here this string should be come from the above d date/or dateStr String.

1
  • I doubt that you can do this reliably for all formats, e.g. what format is 09/08/07? Commented Jun 6, 2015 at 8:14

2 Answers 2

1

You can do it by using momment.js

http://momentjs.com/downloads/moment.js

  van date=new Date(date);
  var dateInFormate=moment(date);
  var date=dateInFormate.format('yyyy-MM-ddTHH:mm:ssZ'); 
Sign up to request clarification or add additional context in comments.

Comments

1

As @Rob said, there is doubt on the reliably for all formats. What you need is pre defined map with key being the format and value being its corresponding regular expression.

Now, create a function with input as dateStr and will return the format. Like

function getDateFormat(dateStr) {
 var format = default_format;
 // Check in map for format
 // If you get the format in map, return that else return a default format.
 return format;
} 

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.