Jose
Jose

Reputation: 1829

Convert Date with Angular 4, Jhipster 4.x

I have a property on my object, I want to save a date with time

public fechaActualizacion?: any,

I initialize it like this:

this.oportunidad.fechaActualizacion = new Date();

But turning it into service gives me an error

 copy.fechaActualizacion = this.dateUtils.toDate(oportunidad.fechaActualizacion);

toDate:

function toDate(date) {
    if (date === undefined || date === null) {
        return null;
    }
    var dateParts = date.split(/\D+/);
    return new Date(dateParts[0], dateParts[1] - 1, dateParts[2], dateParts[3], dateParts[4]);
};

Error:

ERROR TypeError: t.split is not a function

Upvotes: 0

Views: 572

Answers (3)

Philip John
Philip John

Reputation: 5565

Instead of new Date() you can use new Date().toISOString().replace( 'Z', '' ). That will fix the issue.

i.e.

this.oportunidad.fechaActualizacion = new Date();

will become

this.oportunidad.fechaActualizacion = new Date().toISOString().replace( 'Z', '' );

Upvotes: 1

freemanpolys
freemanpolys

Reputation: 1998

Use ZonedDateTime for your entity.

If you don't want time on your Angular views, you just have to apply date filter.

Upvotes: 1

Gaël Marziou
Gaël Marziou

Reputation: 16294

You're passing a Date object to a method that expects a string to build a Date object. There should be more explicit typing in calling code and in toDate().

Upvotes: 0

Related Questions