Format JS datetime to correct ISO format

angular asp.net-core entity-framework-core postgresql typescript

Question

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)

1
0
1/2/2019 9:52:57 PM

Accepted Answer

So I honestly forgot I'd opened this question. I asked again 6 months later and got the answer I was looking for:

Parse JS Date.toIsoString in C#

In the end the answer was just using Convert.ToDateTime() and it gave me the exact date format I needed.

Closing this question!

0
9/4/2019 7:23:51 PM

Popular Answer

use unix_timestamp As a basic format and convert them according to you need

get unix timestamp in c# :

var timestamp = new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds();

get Datetime from unix timestamp in c# :

DateTime dt = DateTimeOffset.FromUnixTimeSeconds(1549741828).DateTime;

get unix timestamp in javascript :

let timestamp = parseInt(new Date().getTime()/1000);

get Datetime from unix timestamp in c# :

let date = new Date(1549742450 * 1000)


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow