Angular convert date

angular 7 date format
angular 4 date format in component
angular date pipe example
angular convert date to utc
datepipe.transform angular 6
unable to convert into a date' for pipe 'datepipe
angular timezone
angular timezone conversion

I have an input field called month.

  <span>
        Month: <br />
        <input id="typeahead-basic" type="text" class="form-control month" />
    </span>

What I want to do is, if you type in 6 in the month field, it will automatically change to 06, if you type 2, it will change to 02. So everything from 1-9 should add a zero. Would be nice if I could implement this into the component.ts class. Thank you very much!

Edit: So what I did was: This in the component:

 checkMonth() {
    if (this.month < 1) {
        this.month = "01";
    } else if (this.month > 12) {
        this.month = 12;
    } else if (this.month < 10) {
        this.month = '0' + this.month;
    }
}

And this as the HTML code:

<input class="form-control hideSpinners" min=1 max=12 type="number" [(ngModel)]="month"
                (change)="checkMonth()"/>

Impossible with simple HTML.

What you can do is call a function onChange when your value changes.

<span>
    Month: <br />
    <input id="typeahead-basic" type="text" class="form-control month" onchange="addZero(this)"/>
</span>

In your component script

addZero(input) {
    if(input.value > 0 && input.value < 10) input.value = `0${input.value}`;
}

Example : http://jsfiddle.net/nrjw56sv/ (Vanilla JS) https://stackblitz.com/edit/angular-fr19pk (Angular)

how to convert current date to YYYY-MM-DD format with angular 2 , Example as per doc @Component({ selector: 'date-pipe', template: `<div> <p>​Today is {{today | date}}</p> <p>Or if you prefer, {{today  This date filter does not, however, convert the date to your browser locale or timezone. Further, the timezone is not presented as a string so that’s not presentation friendly. Assuming you are following the practice of storing dates in UTC format, you’ll likely want to present dates to the user in their own timezone.

slice with a negative value will do the trick just fine :

const input = document.querySelector('input[type="text"]#id');

input.addEventListener('blur', () => {
  input.value = input.value.length === 1 ? 
    `0${input.value}`.slice(-2) :
    input.value;
});
<input id="id" type="text" placeholder="Enter a numeric value">
<input type="text" placeholder="Enter a numeric value (HTML only)" onblur="this.value = this.value.length === 1 ? ('0' + this.value).slice(-2) : this.value">

formatDate, The date-time components to include. See DatePipe for details. locale, string. A locale code for the locale format rules to use. The date filter formats a date to a specified format. The date can be a date object, milliseconds, or a datetime string like "2016-01-05T09:05:05.035Z" By default, the format is "MMM d, y" (Jan 5, 2016).

You could use the ngModel directive. So something like:

  <span>
        Month: <br />
        <input name="inputfield" #input [(ngModel)]="month" (input)="changeDate(month)" id="typeahead-basic" type="text" class="form-control month" />
    </span>

You'll need a public property in your component that matches the ngModel, so "month" in this case.

Then you'd just need to write a changeDate function that takes the ngModel value from the input field and adds the 0 to it, which is straight forward enough in vanilla javascript.

DatePipe, The date/time components to include, using predefined options or a custom format string. Optional. Default is 'mediumDate' . timezone, string. A timezone offset  This is date from database: 08/08/2019 12:22:48 PM UTC? – OPV Aug 8 '19 at 9:23. @OPV you can write any format you want. The main requirement is just add UTC. – StepUp Aug 8 '19 at 9:25. 1. It does not work for me, it gives UTC format: <p>The date is { {today | date:'yyyy-MM-dd HH:mm:ss'}}</p> – OPV Aug 8 '19 at 9:52.

Angular Date Pipe & formatting dates in Angular with examples , Using date pipe, we can convert a date object, a number (milliseconds from UTC) or an ISO date strings according to given predefined angular  I want to create a new Date object, with a specific date. I thought of converting it from a specific string, for example: let dateString = '1968-11-16T00:00:00' How can I convert it to a date ob

Convert Date To String in TypeScript, In TypeScript, the toDateString() method converts the date of a Date object into a readable string, but it does not convert the time. Syntax. Date. if your goal is to convert a date string into a date (as per the OP "How do I convert it to a date object?"). share | improve this answer | follow | answered May 2 '16 at 15:53

Convert string to date or date to string in typescript/javascript with , Every application, we want to manipulate dates and convert to/from date to We will see the example to convert Date to string or String to Date in typescript How to convert/parse json to/from object in angular with examples| parse json in​  These examples transform a date into various formats, assuming that dateObj is a JavaScript Date object for year: 2015, month: 6, day: 15, hour: 21, minute: 43, second: 11, given in the local time for the en-US locale.

Comments
  • use native javascript '9'.padStart(2,0)
  • How can I implement this in the component?
  • Bind to (input) event and add the padding. You can use other events for different behaviors. (input) triggers when a key is pressed. (change) triggers when enter is pressed. (blur) triggers when focus is moved.
  • Thank you very much, do I need to include the addZero function into what is called export class? Because I did that and it didnt work.
  • I don't know your architecture, but I suppose
  • I added the function and it tells me the function is unused, pretty weird.
  • Unused or undefined ?
  • It tells me it is unused