Da alcuni giorni cerco di ottenere il mio codice lato server (C #) per salvare un timestamp inviato tramite una chiamata API a Postgres.
Sembra funzionare bene se collego DateTime.Now()
all'oggetto di trasferimento dati ma quando provo e analizzo un datetime inviato dal client ottengo un 400 con
**0: "The input was not valid."**.
Come mia risposta
Le mie lezioni assomigliano a questo
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; }
}
Metodo che mappa il nuovo modulo (con data di uscita) (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}`
};
}
Formato della data dalla richiesta: "2019-01-04T00: 00: 00.000Z" Formato richiesto: "2018-12-29 20: 23: 22.667766" <- Le date sono memorizzate in PG come timestamp senza fuso orario
La mia domanda è, qualcuno sa quale formato dovrei inviare la mia data per eseguire il back-end (ho sentito che è ISO 8601)? Dovrei anche fare qualcosa per la data sul client? E come posso convertire la mia data JS in una data che salverà in PG?
Sto anche usando Microsoft Entity Framework Core con Postgres (se questo aiuta)
Quindi ho sinceramente dimenticato di aver aperto questa domanda. Ho chiesto di nuovo 6 mesi dopo e ho ottenuto la risposta che cercavo:
Parse JS Date.toIsoString in C #
Alla fine la risposta stava usando solo Convert.ToDateTime()
e mi ha dato il formato esatto della data di cui avevo bisogno.
Chiudendo questa domanda!
usa unix_timestamp come formato di base e convertili in base alle tue necessità
ottenere un timestamp unix in c #:
var timestamp = new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds();
get Datetime da unix timestamp in c #:
DateTime dt = DateTimeOffset.FromUnixTimeSeconds(1549741828).DateTime;
ottenere un timestamp unix in javascript:
let timestamp = parseInt(new Date().getTime()/1000);
get Datetime da unix timestamp in c #:
let date = new Date(1549742450 * 1000)