I've been trying for a few days now to get my server side code (C#) to save a timestamp sent via an API call to Postgres.
It seems to work fine if I attach DateTime.Now() to the data transfer object but when I try and parse a datetime sent from the client i get a 400 with
**0: "The input was not valid."**.
As my response.
My classes look like this
TS:
export interface ArtistShows {
showDate: string;
venue: string;
id?: string;
bandsAlsoPlaying: Array<string>;
}
C#
public class ArtistShow
{
public Guid Id { get; set; }
public DateTime ShowDate { get; set; }
public string Venue { get; set; }
public string BandsAlsoPlaying { get; set; }
public virtual Artist Artist { get; set; }
}
Method that maps new form (w/ouputted Date) (TS)
private _mapNewShowForm(): ArtistShows {
let showDate = this.newShowForm.get(this.newShowFormEnum.DATE).value;
let convertedShowDate = new Date(showDate);
return {
bandsAlsoPlaying: [],
showDate: convertedShowDate.toISOString(),
venue: `${this.newShowForm.get(this.newShowFormEnum.VENUE).value}, ${this.newShowForm.get(this.newShowFormEnum.CITY).value}`
};
}
Date format from request: "2019-01-04T00:00:00.000Z" Required format: "2018-12-29 20:23:22.667766" <-- Dates are stored in PG as timestamp without timezone
My question is, does anyone know what format I should be sending my date to backend in (I heard it's ISO 8601)? Should I even be doing anything to the Date on the client? And how can I convert my JS date to a date that will save in PG?
I am also using Microsoft Entity Framework Core with Postgres (if that helps)
moment.jslib. It's perfect for working with datetime. momentjs.com/docs/#/displaying/format - docs for formatting withmoment.js. By defaultmoment().format()returns current datetime in ISO 8601.toISOStringmethod returns ISO8601 format of date.getTimemethod developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/….date-fnsovermoment.js, as the latter is a resource hog dependency-wise.