1

I'm trying to hit a soccer sports API which includes date in the format of yyyy-mm-dd, only the scores from that date to current date will be displayed. The current date is chosen by user using a calendar but when the user chooses the date from calendar, it gets displayed in ISO format as "Fri Aug 17 2018 00:00:00 GMT +0545 (Nepal Time)" . I want to convert this date in the front end in the yyyy-mm-dd format and send it to the API Url in back end. I'm using AngularJS and Java. How do I convert the full ISO date into that format?

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117
Bikal Nepal
  • 367
  • 4
  • 18
  • 1
    Use built in date filter – charlietfl Aug 15 '18 at 11:34
  • 1
    “I’m using AngularJS and Java.” Did you mean JavaScript rather than Java? http://javascriptisnotjava.io/ – Basil Bourque Aug 15 '18 at 15:45
  • "Fri Aug 17 2018 00:00:00 GMT +0545 (Nepal Time)" is not "ISO format". – RobG Aug 16 '18 at 02:26
  • @BasilBourque No. I'm using Java on the backend and AngularJS for the frontend. Generated the project using JHipster. – Bikal Nepal Aug 16 '18 at 04:42
  • @RobG Kindly tell me what format is it called as I couldn't find a name for it online. – Bikal Nepal Aug 16 '18 at 04:43
  • 1
    It's the format for [*Date.prototype.toString*](http://ecma-international.org/ecma-262/9.0/#sec-date.prototype.tostring) introduced in ECMAScript 2018, so you might call it the ECMAScript format. ;-) It's similar to the [*RFC2822* format](http://tools.ietf.org/html/rfc2822#section-3.3). Using moment.js tokens, RFC2822 is `ddd, DD MMM YYYY HH:mm:ss ZZ` whereas ECMAScript format is `ddd MMM DD YYYY HH:mm:ss.SSS ZZ (timezoneName)`, where "ZZ" is "GMT±HHmm" or "Z", the timezone HHmm is the offset hours and minutes and the timezoneName is implementation dependent. – RobG Aug 16 '18 at 09:09

2 Answers2

1

Based on that output it sounds like your date is stored as a JavaScript date object (see: https://www.w3schools.com/js/js_dates.asp)

To get the string you want, one solution would be to take the value of your input (I'll call it d) and do the following (I assume you have momentjs loaded:

var datestring = moment(d).format('YYYY-MM-DD')

datestring should now include the date in the format you want... if for some reason d is a string instead of a date object, you can create a parsing pattern following the momentjs doc here: https://momentjs.com/docs/#/parsing/

Campbell
  • 326
  • 2
  • 8
1

Assuming you have a JavaScript Date object to work with, you can do this in plain JS:

var datestring = dateobj.toISOString().substring(0, 10);   // 'yyyy-MM-dd'

If you only have the display string ("Fri Aug 17 2018 00:00:00 GMT +0545 (Nepal Time)"), you can first convert that into a Date object with this:

// displaystring = "Fri Aug 17 2018 00:00:00 GMT +0545 (Nepal Time)";
var dateobj = new Date(displaystring);

...and then do the datestring conversion above.

David R Tribble
  • 10,646
  • 4
  • 39
  • 50
  • *toISOString* will return the UTC date, which will be different to the local date for the duration of the local timezone offset either before or after midnight (depending on whether the host is west or east of Greenwhich respectively). – RobG Aug 16 '18 at 02:24