2

i have some trouble properly using date with angular5

i fetch data from a mocked api where date is generated like that : 24/07/2018 00:00:00 so there is no problem with resarch or anything manipulating those dates.

sample of the generation in api mock :

 for (int i = 1; i <= NUMBER_OF_POLICIES; i++)
            {
                Policy lPolicy = new Policy { Id = i, Name = string.Concat("Policy number ", i), Date = DateTime.Now.AddDays(i) };

                p.Add(lPolicy);
            }

problem comes when i update dates with angular material DatePicker component.

with the picker, date shows up like tis :

Mon Jul 30 2018 00:00:00 GMT+0200

so when i update the item, date is passed like this :

updated item: {"id":1,"name":"Policy number 1","date":"2018-07-29T22:00:00.000Z"}

any idea how to deal with locales ?

EDIT :

Here is the problem when i do some research :

enter image description here

5
  • use angular filter Commented Jul 24, 2018 at 8:46
  • how you want the date to be displayed ? what's the format you want ? Commented Jul 24, 2018 at 8:56
  • helle @SaurabhAgrawal, do you mean filters like {{ today | date }}? if so, it's okay for displaying the date, but not to manipulate it :/ for example, if i update the date to 31/07/2018, it will be corretly displayed in browser, but if i use the "search" bar looking 31/07/2018, i dont get the item. to get it i have to search for 30/07/2018 even if displayed at 31/07/2018 Commented Jul 24, 2018 at 8:57
  • you can inject the DatePipe into your component and use it to manipulate date as you want Commented Jul 24, 2018 at 8:59
  • i just edited my post so you can actually see the result : policy1 and 2 have been updated with DatePicker Commented Jul 24, 2018 at 9:06

3 Answers 3

2

You can use DatePipe in the component itself, and format the date using pre-defined-format-options

Try this

import { DatePipe } from '@angular/common';

class Service {    
  constructor(private datePipe: DatePipe) {}    
  changeDate(date) {
    return this.datePipe.transform(date, 'yyyy-MM-dd');
  }
}

Or you can format date like this as well

var formatted = new DatePipe().transform(raw, 'yyyy-MM-dd');
Sign up to request clarification or add additional context in comments.

6 Comments

Mh, i've have an issue trying to inject the DatePipe in constructor. i get the following error : ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[DatePipe]: StaticInjectorError(Platform: core)[DatePipe]: NullInjectorError: No provider for DatePipe!
Nice, it seems to work ! thanks @Saurabh ! but it means that i have to manually pass the local into the pipe, right? like : const formatted = new DatePipe('en-EN').transform(p.date, 'yyyy-MM-dd'); so what about internationalization?
@j0w try this doc angular.io/guide/i18n.. Hope it will help you.
i sure will! :) if you could just tell me if the answer i posted is also a viable solution, and why i should use one over the other, that would help me a lot! thanks for your time! :)
Sure i will just give me sometime. I like the way dedicated you are.. Tomorrow I will answer you this one.
|
1

use moment for date and time, which gives the necessary formats.

https://momentjs.com/

momentjs have different properties to get the time and date in necessary format.

newDate = this.Today

date = moment(this.newDate)

you can also pass necessary formats like

date = moment(this.newDate).format('DD/MM/YYYY')

Comments

0
 const offset = p.date.getTimezoneOffset();
 const test = p.date.setMinutes(p.date.getMinutes() - offset);

console.log(test) returns :

updated item: {"id":1,"name":"Policy number 1","date":"2018-07-31T00:00:00.000Z"}

it works too but, is that "clean"?

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.