0

I am getting data through Django api into my web application and the date return is "2015-09-10T00:00:00Z", is there a javscript function that can turn this into format like 2015-09-10

Nakib
  • 4,593
  • 7
  • 23
  • 45
  • you could format it server side, this post might help http://stackoverflow.com/questions/8287883/django-how-to-get-format-date-in-views – joeskru Oct 08 '15 at 20:10

1 Answers1

2
var date = new Date("2015-09-10T00:00:00Z");
var result = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();

This is cumbersome and error prone, also it removes the leading 0 for the month and day. Take a look at this js library: mement.js, it should provide whatever you need to manipulate the time. It's very light weight and easy to use.

Shang Wang
  • 24,909
  • 20
  • 73
  • 94
  • is there any javascript function because for one single task i dont wanna include whole library – Nakib Oct 08 '15 at 20:13
  • I updated my answer, but I still suggest using the library because, well, the library exists for a reason, there's no built in javascript function that handles this well. It's also light weight so there's no overhead. Check this answer to see how painful it is to manipulate the string: http://stackoverflow.com/questions/3605214/javascript-add-leading-zeroes-to-date – Shang Wang Oct 08 '15 at 20:33