0

Trying to figure out how to convert a date that is in the form of a string into a date that calculations can be run on. See below.

function PerishableFood(name,lastBuyDate,quantity, expirationDate) {
   this.item = name;
   this.buyDate = lastBuyDate;
   this.quantity = quantity;
   this.expireDate = expirationDate;
    }
  var milk = new PerishableFood('1% Milk','07/05/2017',1, '07/17/2017');
  var butter = new PerishableFood('Test', '07/05/2017',2,'8/30/2017');

I know how to transform dates this way (see below) but the above are strings within an object from a user input so I am not sure how to transform them.

var date1 = '01/12/2018';
var date1 = new Date(date1);

Sorry if this is super easy not an expert yet in JS.

desky
  • 13
  • 3
  • i dont see a question. Are you asking how to copy the second code block into the first or what? – Jonas Wilms Jul 16 '17 at 13:33
  • `this.buyDate = new Date(lastBuyDate);` and so on – Rahul Arora Jul 16 '17 at 13:33
  • Possible duplicate of [Converting string to date in js](https://stackoverflow.com/questions/5619202/converting-string-to-date-in-js) – Alexander Jul 16 '17 at 13:36
  • The second block of code is the only thing that I know about how to convert a string to a date. But I honestly don't understand how that example of converting it as a variable works when your string is part of an object that you want to be converted. The idea is to be able to call the expiration date and compare it to the current date to see if it is expired but it can't be done to my understanding if it is a string. It has to be a date but the user input for creating it makes it a string to begin with. – desky Jul 16 '17 at 13:40

1 Answers1

0

You can access expireDate like this milk.expireDate

Here is a working fiddle

function PerishableFood(name,lastBuyDate,quantity, expirationDate) {
   this.item = name;
   this.buyDate = lastBuyDate;
   this.quantity = quantity;
   this.expireDate = expirationDate;
    }
  var milk = new PerishableFood('1% Milk','07/05/2017',1, '07/17/2017');
  var butter = new PerishableFood('Test', '07/05/2017',2,'8/30/2017');


var date1 = new Date(milk.expireDate);
console.log(date1);
Kukic Vladimir
  • 1,010
  • 4
  • 15
  • 22
  • Is it required that I do that for every item created? For example if you added cheese and bread or over time 100 items. Is there a way to change all of them for all that are created? – desky Jul 16 '17 at 13:42
  • well you can do that when creating `PerishableFood`, simply assign `this.expireDate = new Date(expirationDate)`... It all depends on a workflow you are trying to achieve – Kukic Vladimir Jul 16 '17 at 13:46
  • That's exactly what I was hoping to do. Change it from the beginning that way I don't have to worry about it after that. – desky Jul 16 '17 at 13:52
  • When I run this in console.log I get the following error: [object Date] { ... } ... my code is: this.buyDate = new Date(lastBuyDate); console.log(milk.buyDate); – desky Jul 16 '17 at 15:59
  • So when I took the advice to do the adjustment when creating PerishableFood with this.expireDate = new Date(expirationDate) I get that odd looking error. Is that because it is looking at the string and is not converting it? – desky Jul 16 '17 at 16:02
  • @desky here is a working [fiddle](https://jsfiddle.net/kukicvladimir/8ckyago0/2/) – Kukic Vladimir Jul 16 '17 at 16:04