简体   繁体   中英

how to add years in date in angular js in the html

I have this code on my html

<p>Date issued: <span class="b">{{orderPrint.orderDetails.received_date | date : "medium"}}</span></p>

It displays something like this: Date: Mar 16, 2018 7:39:08 PM

I want to display Date: Mar 16, 2023 7:39:08 PM. how can I do that?

We can do this using setYear() in Date.

For eg.

var myDate = new Date();

and +1 to add a year in current date.

myDate.setYear(userdob.getFullYear() + 1);

In your case, just update your date value as below.

var updatedYear = orderPrint.orderDetails.received_date.getFullYear() + 5;
orderPrint.orderDetails.received_date.setYear(updatedYear);

Usage In HTML Template Binding:

{{ date_expression | date : format : timezone}}

Example:

<span ng-non-bindable>{{1288323623006 | date:'medium'}}</span>:
<span>{{1288323623006 | date:'medium'}}</span><br>
<span ng-non-bindable>{{1288323623006 | date:'yyyy-MM-dd HH:mm:ss Z'}}</span>:
<span>{{1288323623006 | date:'yyyy-MM-dd HH:mm:ss Z'}}</span><br>
<span ng-non-bindable>{{1288323623006 | date:'MM/dd/yyyy @ h:mma'}}</span>:
<span>{{'1288323623006' | date:'MM/dd/yyyy @ h:mma'}}</span><br>
<span ng-non-bindable>{{1288323623006 | date:"MM/dd/yyyy 'at' h:mma"}}</span>:
<span>{{'1288323623006' | date:"MM/dd/yyyy 'at' h:mma"}}</span><br>

For more details: https://docs.angularjs.org/api/ng/filter/date

If you want to add timezone then use this

new Date().toLocaleString("en-US", {timeZone: "America/Los_Angeles"})

if you want to just add year/month/time in date. then follow this steps

step 1- first need to parse date. use

var a = data.parse(new date());

step 2 - a = a + (365*24*60*60*1000)

if you want to add year follow this javascript

   var dat=new Date();
    var y=dat.getFullYear()+5;
    dat.setFullYear(y);

and amol bhor also fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM