0

I am using the Below function to generate the Date and Time in my angular app but it's returning as a string instead of date&time dataType.

getCurrentDateAndTime() {
  const today = new Date();
  const datePipe = new DatePipe('en-IN');
  return datePipe.transform(today, 'dd/MM/yyyy, hh:mm a');
}

can suggest doing better way for generating Date datatype in the format 'dd/mm/yyyy , hh:mm am'

adiga
  • 34,372
  • 9
  • 61
  • 83
Shubham Singh
  • 147
  • 1
  • 2
  • 15

3 Answers3

1

Why are you using the datePipe.transform method? It's job is to create a string for representation so if you want a Date returned, you don't need the datePipe, you can just return new Date() (ofc you don't need a method for that)

Regarding your edit, you don't keep date objects in a specific format, it just keeps the information about the point in time, you usually only apply a format once you want to display it and that's when the datePipe.transform method comes into play.

Bernhard
  • 1,253
  • 8
  • 18
  • i need date in 'dd/mm/yyyy , hh:mm am' format as Date datatype – Shubham Singh Dec 23 '20 at 06:41
  • @ShubhamSingh—that's a non–sequitur. 'dd/mm/yyyy , hh:mm am' is a string format, a Date is an object that has a single data value: an offset in milliseconds since the ECMAScript epoch. It also has some methods for various functions. That's it. It does not store format, timezone or colour of the sky. – RobG Dec 23 '20 at 22:29
0

Date type value is numeric internally. The string representation can vary according to your choice. In your case:

const d = new Date();
const dAsString = d.toLocaleString('en-IN');

Result: "23/12/2020, 8:46:27 am"

Benny Halperin
  • 1,872
  • 3
  • 13
  • 18
  • I need output as a Date object but in your code snippet it's giving type as a string I need type as a Date object – Shubham Singh Dec 23 '20 at 07:03
  • @ShubhamSingh I'm saying that the Date object you need is `const today = new Date()`. Any string representation will never be the Date object. It's not clear to me what you really need. Maybe if you can post the full code. – Benny Halperin Dec 23 '20 at 07:59
0

May be another way using getTime()You have to use ISO only. See this Stackoverflow Link

What is the "right" JSON date format?

import { DatePipe } from "@angular/common";
import { Component, VERSION } from "@angular/core";

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"],
  providers: [DatePipe]
})
export class AppComponent {
  name = "Angular " + VERSION.major;

  dataFormatted: string = "";
  dateInMilliSecond: number;
  dataISO: string = '';

  constructor(protected datePipe: DatePipe) {}

  ngOnInit() {
    const today = new Date();
    const datePipe = new DatePipe("en-IN");
    this.dataFormatted = datePipe.transform(today, "dd/MM/yyyy, hh:mm a");
    this.dateInMilliSecond = new Date().getTime();
    this.dataISO = new Date().toISOString();
  }
}


{{dataFormatted}}
<br/>
<br/>
In MilliSecond: {{dateInMilliSecond | date: 'dd/MM/yyyy, hh:mm a'}}
<br/>
<br/>

ISO Date : {{dataISO}}
praga2050
  • 729
  • 9
  • 18