Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
188
rated 0 times [  191] [ 3]  / answers: 1 / hits: 18402  / 5 Years ago, fri, january 18, 2019, 12:00:00

I am developing in Angular 6. I keep form values as json in a database when new record is saved.If end user wants to show exists record , I fill form components from json data.But I got into trouble for casting date values . I wasn't be able to cast correctly my local date.
I have tried with moment,but didn't work:


console.log("string Value",stringValue);
let date = moment(stringValue,"yyyy-mm-ddThh:mm:ss.fffZ");
console.log("date",date.format('DD/MM/YYY HH:mm:ss'));


string Value output: 2019-01-17T21:00:00.000Z


console output actual : date 18/01/2019 01:00:00


but console output expected : date 18/01/2019 00:00:00



I tried "YYYY-MM-DDThh:mm:ss.fffZ" but didn't work too.


EXTRA INFORMATION


saving data:


process.data = JSON.stringify(this.form.getRawValue());
save(process);

html(primeng):


<p-calendar formControlName="startDate" dateFormat="dd.mm.yy"></p-calendar>

More From » typescript

 Answers
15

You can parse your '2019-01-17T21:00:00.000Z' input using moment.utc() since it represents time in UTC




By default, moment parses and displays in local time.



If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment().




and then convert it to local timezone using local().



Here a live sample:





const stringValue = '2019-01-17T21:00:00.000Z';
let date = moment.utc(stringValue).local();
console.log(date, date.format('DD/MM/YYYY HH:mm:ss'));

<script src=https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js></script>




[#52755] Friday, January 11, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
calicinthias

Total Points: 447
Total Questions: 101
Total Answers: 118

Location: Botswana
Member since Sat, Dec 31, 2022
1 Year ago
;