0

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)

5

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.