Search code examples
node.jsangularserver

formatting mat datepicker angular and sending to node js


I wanna send date to node to store but 2022-04-26T18:30:00.000Z this is the format I am getting I wanna get like this 26-04-2022

My Angular HTML

<mat-form-field color="accent" appearance="fill">
  <mat-label>Custom calendar color</mat-label>
  <input matInput [matDatepicker]="picker2">
  <mat-datepicker-toggle matSuffix [for]="picker2"></mat-datepicker-toggle>
  <mat-datepicker #picker2 color="primary"></mat-datepicker>
</mat-form-field>

how should I change it should I do it in angular or on node.js API. To be precise I am sending more values from angular like having some extra Input box and sending as form value. If I wanna Do this in api how should i separate the date and format it

My API

app.post('/path', function (req, res){
      var data = req.body;
var onedata;
   fs.readfile('filepath', 'utf8', function(err, data){
     if(data){
        onedata = JSON.parse(data)
        onedata.push(data)
}
fs.writefile('filepath', 'utf8', function(err, data){
     if(err){
        throw err;
          res.send("success");
}

above api is how i wrote in my file

Full HTML angular for reference

<form>
  <mat-form-field appearance="outline">
    <mat-label>Outline form field</mat-label>
    <input matInput placeholder="Placeholder">
    <mat-icon matSuffix>sentiment_very_satisfied</mat-icon>
    <mat-hint>Hint</mat-hint>
  </mat-form-field>

  <mat-form-field appearance="outline">
    <mat-label>Outline form field</mat-label>
    <input matInput placeholder="Placeholder">
    <mat-icon matSuffix>sentiment_very_satisfied</mat-icon>
    <mat-hint>Hint</mat-hint>
  </mat-form-field>

  <mat-form-field appearance="outline">
    <mat-label>Outline form field</mat-label>
    <input matInput placeholder="Placeholder">
    <mat-icon matSuffix>sentiment_very_satisfied</mat-icon>
    <mat-hint>Hint</mat-hint>
  </mat-form-field>

<mat-form-field color="accent" appearance="fill">
  <mat-label>Custom calendar color</mat-label>
  <input matInput [matDatepicker]="picker2">
  <mat-datepicker-toggle matSuffix [for]="picker2"></mat-datepicker-toggle>
  <mat-datepicker #picker2 color="primary"></mat-datepicker>
</mat-form-field>
</form>


Solution

  • You want to install moment js

    npm install moment --save
    

    It will help you to format your date however you want You can see more here --->> moment js