{ Community }
  • Academy
  • Docs
  • Developers
  • Resources
    • Community Articles
    • Apigee on GitHub
    • Code Samples
    • Videos & eBooks
    • Accelerator Methodology
  • Support
  • Ask a Question
  • Spaces
    • Product Announcements
    • General
    • Edge/API Management
    • Developer Portal (Drupal-based)
    • Developer Portal (Integrated)
    • API Design
    • APIM on Istio
    • Extensions
    • Business of APIs
    • Academy/Certification
    • Adapter for Envoy
    • Analytics
    • Events
    • Hybrid
    • Integration (AWS, PCF, Etc.)
    • Microgateway
    • Monetization
    • Private Cloud Deployment
    • 日本語コミュニティ
    • Insights
    • IoT Apigee Link
    • BaaS/Usergrid
    • BaaS Transition/Migration
    • Apigee-127
    • New Customers
    • Topics
    • Questions
    • Articles
    • Ideas
    • Leaderboard
    • Badges
  • Log in
  • Sign up

Get answers, ideas, and support from the Apigee Community

  • Home /
  • Edge/API Management /
avatar image
0
Question by shubham singh · Dec 22, 2017 at 09:30 AM · 61.2k Views javascript

how to write a javascript for subtracting days from an extracted date?

I will get the date from the response of a service in the format YYYY-MM-DD .After extracting the date i need to subtract 28days from the extracted date.How to do it via javascript?

The code which i wrote is below

1st scenario:-

var ss=""
var date="12/22/2017"
var now=new Date(date);

ss=now.setDate(now.getDate()-30)


Output=1511308800000

2nd scenario:-

if i put the code as
var date="12/22/2017"
var now=new Date(date);
now.setDate(now.getDate()-30)

output=Wed Nov 22 2017 00:00:00 GMT-0000 (UTC)

in the 1st scenario,the output which i am getting is this because of i ma setting that to a variable?

how should i assign in to a different variable?
is there a better way to do this? Any help on this?
Comment
Add comment
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Close

1 Answer

  • Sort: 
avatar image
0
Best Answer

Answer by Dino   · Dec 22, 2017 at 04:21 PM

The JavaScript Date type has a getDate() method, which returns an integer number, between 1 and 31, representing the day of the month for the given date according to local time.

If you want a date from 28 days prior, you would do this:

var dateString = "12/22/2017";
var date1 = new Date(dateString);
var daysPrior = 28;
date1.setDate(date1.getDate() - daysPrior));
print (date1.toISOString()); 

Subtracting days may give you a "negative date", but the setDate() function allows for that and will adjust the month accordingly.

This is independent of Apigee Edge or Rhino. This works in any JavaScript engine.

Comment
Add comment Show 1 · Link
10 |5000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by Apigeeks only
  • Viewable by the original poster
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mauricio Navarro Miranda · Dec 22, 2017 at 09:38 PM 0
Link

This works great.

If you make a little change, you'll get an even broader solution

var daysToMove = -28;
date1.setDate(date1.getDate() + daysToMove);


Reference to Date.prototype.setDate() if in doubt


https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate

Follow this Question

Answers Answers and Comments

63 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Javascript: context.proxyRequest.url ===context.targetRequest.url ???? 1 Answer

Modifying proxy.pathsuffix to trim excess 1 Answer

Unable to decode queryparam 0 Answers

Mask the keys / password values before logging. 2 Answers

How to modify request verb from POST to GET? 3 Answers

  • Products
    • Edge - APIs
    • Insights - Big Data
    • Plans
  • Developers
    • Overview
    • Documentation
  • Resources
    • Overview
    • Blog
    • Apigee Institute
    • Academy
    • Documentation
  • Company
    • Overview
    • Press
    • Customers
    • Partners
    • Team
    • Events
    • Careers
    • Contact Us
  • Support
    • Support Overview
    • Documentation
    • Status
    • Edge Support Portal
    • Privacy Policy
    • Terms & Conditions
© 2021 Apigee Corp. All rights reserved. - Apigee Community Terms of Use - Powered by AnswerHub
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Create an article
  • Post an idea
  • Spaces
  • Product Announcements
  • General
  • Edge/API Management
  • Developer Portal (Drupal-based)
  • Developer Portal (Integrated)
  • API Design
  • APIM on Istio
  • Extensions
  • Business of APIs
  • Academy/Certification
  • Adapter for Envoy
  • Analytics
  • Events
  • Hybrid
  • Integration (AWS, PCF, Etc.)
  • Microgateway
  • Monetization
  • Private Cloud Deployment
  • 日本語コミュニティ
  • Insights
  • IoT Apigee Link
  • BaaS/Usergrid
  • BaaS Transition/Migration
  • Apigee-127
  • New Customers
  • Explore
  • Topics
  • Questions
  • Articles
  • Ideas
  • Badges