<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Probably a newb question, but, date formats... help? in Data Hub</title>
    <link>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407688#M65</link>
    <description>&lt;P&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/131313"&gt;@SaleProcessNerd&lt;/a&gt;&amp;nbsp;- dates are always fun to work with &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try this out:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;let dateString = 1614470400000;
let newDate = addMonths(dateString,12);
console.log(newDate); // just to check

newDate = newDate.getTime()
console.log(newDate);

function addMonths(date, months) {
    var od = new Date(date);
  	var nd = new Date(date);    
    var od_m = od.getMonth();
    od.setMonth(od_m + months);
    if (od.getDate() != nd) {
      od.setDate(0);
    }
    return od;
}&lt;/LI-CODE&gt;</description>
    <pubDate>Tue, 09 Feb 2021 18:14:45 GMT</pubDate>
    <dc:creator>sam-g</dc:creator>
    <dc:date>2021-02-09T18:14:45Z</dc:date>
    <item>
      <title>Probably a newb question, but I need help with date formats... help?</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/406879#M57</link>
      <description>&lt;P&gt;Off the start, I thank you for your patience. I am a sales guy that talked himself into a RevOps role and I'm literally breaking my teeth in coding wise with the coded workflow feature.&lt;BR /&gt;&lt;BR /&gt;So......&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm trying to take a date that was copied into a custom date field ( 'test') , add a year to it, and update that field (in a deal).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;const hubspot = require('@hubspot/api-client');

exports.main = (event, callback) =&amp;gt; {
  callback(processEvent(event));
};

function processEvent(event) {
  const hubspotClient = new hubspot.Client({ apiKey: process.env.WhiteRabbit });

  let dealId = event.object.objectId;

  hubspotClient.crm.deals.basicApi
    .getById(dealId, ['test'])
    .then(results =&amp;gt; {
     let dateString = results.body.properties['test'];

      hubspotClient.crm.deals.basicApi
        .update(
          dealId,
          {
            properties: {
              ['test']: addYear(dateString)
            }
          }
        )
    });
}
function addYear(dateString) {
const date = new Date(dateString);
const year = date.getFullYear() + 1;
const month = date.getMonth() + 1;
const day = date.getDate();
return date __________?
;
//return    month + '/' + day + '/' + year  -&amp;gt;date format error
//return    `${year}/${month}/${day}`       -&amp;gt;date format error
//return    date.toUTCString()              -&amp;gt; not a function
//return.   date.toISOString()              -&amp;gt; not a function&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can't seem to figure out how to format this new date so hubstop will accept it. I&amp;nbsp; run into:&lt;BR /&gt;&lt;SPAN&gt;[{\“isValid\“:false,\“message\“:\“2022/2/28 was not a valid long......&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#000000"&gt;Help?&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 07 May 2021 14:05:32 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/406879#M57</guid>
      <dc:creator>SaleProcessNerd</dc:creator>
      <dc:date>2021-05-07T14:05:32Z</dc:date>
    </item>
    <item>
      <title>Re: Probably a newb question, but, date formats... help?</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407006#M58</link>
      <description>&lt;P&gt;Can you provide a sample value that you are passing in to this function?&lt;/P&gt;</description>
      <pubDate>Sun, 07 Feb 2021 20:02:35 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407006#M58</guid>
      <dc:creator>sam-g</dc:creator>
      <dc:date>2021-02-07T20:02:35Z</dc:date>
    </item>
    <item>
      <title>Re: Probably a newb question, but, date formats... help?</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407395#M59</link>
      <description>&lt;P&gt;I think what you're looking for is `date.getTime()`. This will return the value as an EPOCH timestamp, which is what HubSpot uses internally for dates!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Feb 2021 22:06:14 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407395#M59</guid>
      <dc:creator>zeke</dc:creator>
      <dc:date>2021-02-08T22:06:14Z</dc:date>
    </item>
    <item>
      <title>Re: Probably a newb question, but, date formats... help?</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407430#M60</link>
      <description>&lt;LI-CODE lang="markup"&gt;"test": {
			"value": "1614470400000",
			"timestamp": 1612500607106,
			"source": "AUTOMATION_PLATFORM",
			"sourceId": "enrollmentId:74194266557;actionExecutionIndex:0",
			"updatedByUserId": null,
			"versions": [
				{
					"name": "test",
					"value": "1614470400000",
					"timestamp": 1612500607106,
					"sourceId": "enrollmentId:74194266557;actionExecutionIndex:0",
					"source": "AUTOMATION_PLATFORM",
					"sourceVid": []
				}
			]&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 09 Feb 2021 00:01:59 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407430#M60</guid>
      <dc:creator>SaleProcessNerd</dc:creator>
      <dc:date>2021-02-09T00:01:59Z</dc:date>
    </item>
    <item>
      <title>Re: Probably a newb question, but, date formats... help?</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407431#M61</link>
      <description>&lt;P&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/131313"&gt;@SaleProcessNerd&lt;/a&gt;&amp;nbsp;- I think there may be a bit of confusion here - you have a value in the `test` field on your deal object that contains a an EPOCH timestamp like in your example above:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;1614470400000&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This value already has a year in it, it's part of the timestamp.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Are you trying to convert this value into just a human readable year and replace your original value in test with that? Or are you trying to extract the year and put it into another field on the record?&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 00:24:54 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407431#M61</guid>
      <dc:creator>sam-g</dc:creator>
      <dc:date>2021-02-09T00:24:54Z</dc:date>
    </item>
    <item>
      <title>Re: Probably a newb question, but, date formats... help?</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407453#M62</link>
      <description>&lt;P&gt;Ultimately I'm trying to take the date value from the field 'Close Date' ( 'closedate' is the HS internal name), add one year to it, and put it into a custom date field 'test' so that new date field 'test' shows a date one year from the Deal's 'Close Date'.&lt;BR /&gt;&lt;BR /&gt;The current code above assumes that the date value from 'Close Date' has been copied into the 'test' field prior to the script step, but it doesnt have to be.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 03:58:31 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407453#M62</guid>
      <dc:creator>SaleProcessNerd</dc:creator>
      <dc:date>2021-02-09T03:58:31Z</dc:date>
    </item>
    <item>
      <title>Re: Probably a newb question, but, date formats... help?</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407639#M63</link>
      <description>&lt;P&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/131313"&gt;@SaleProcessNerd&lt;/a&gt;&amp;nbsp;- I think it would be more efficient to do that all in one coded step (get the close date value and then add the year to the test property), but to just add a year to a date you could use this function:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;let dateString = results.body.properties['test'];
let newDate = addMonths(dateString,12);
console.log(dateString, newDate) // just to check

function addMonths(date, months) {
    var d = date.getDate();
    date.setMonth(date.getMonth() + +months);
    if (date.getDate() != d) {
      date.setDate(0);
    }
    return date;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then just set your test property to the newDate value.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 15:53:39 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407639#M63</guid>
      <dc:creator>sam-g</dc:creator>
      <dc:date>2021-02-09T15:53:39Z</dc:date>
    </item>
    <item>
      <title>Re: Probably a newb question, but, date formats... help?</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407682#M64</link>
      <description>&lt;P&gt;This is the error I got:&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;ERROR Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: date.getDate is not a function","reason":{"errorType":"TypeError","errorMessage":"date.getDate is not a function","stack":["TypeError: date.getDate is not a function","&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;at addMonths&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 17:48:08 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407682#M64</guid>
      <dc:creator>SaleProcessNerd</dc:creator>
      <dc:date>2021-02-09T17:48:08Z</dc:date>
    </item>
    <item>
      <title>Re: Probably a newb question, but, date formats... help?</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407688#M65</link>
      <description>&lt;P&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/131313"&gt;@SaleProcessNerd&lt;/a&gt;&amp;nbsp;- dates are always fun to work with &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try this out:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;let dateString = 1614470400000;
let newDate = addMonths(dateString,12);
console.log(newDate); // just to check

newDate = newDate.getTime()
console.log(newDate);

function addMonths(date, months) {
    var od = new Date(date);
  	var nd = new Date(date);    
    var od_m = od.getMonth();
    od.setMonth(od_m + months);
    if (od.getDate() != nd) {
      od.setDate(0);
    }
    return od;
}&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 09 Feb 2021 18:14:45 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407688#M65</guid>
      <dc:creator>sam-g</dc:creator>
      <dc:date>2021-02-09T18:14:45Z</dc:date>
    </item>
    <item>
      <title>Re: Probably a newb question, but, date formats... help?</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407701#M66</link>
      <description>&lt;P&gt;Well at least I didnt get an error this time, however it didnt add a year to the 'test' field. I appreciate all your help and patience with this.&lt;BR /&gt;&lt;BR /&gt;Also, why have a set value for&amp;nbsp;&lt;/P&gt;&lt;P&gt;let dateString = 1614470400000&lt;/P&gt;&lt;P&gt;&amp;nbsp;Wouldnt I want that to still reference whatever value came from that field?&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 18:45:40 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407701#M66</guid>
      <dc:creator>SaleProcessNerd</dc:creator>
      <dc:date>2021-02-09T18:45:40Z</dc:date>
    </item>
    <item>
      <title>Re: Probably a newb question, but, date formats... help?</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407725#M67</link>
      <description>&lt;P&gt;Yes, sorry I just set a value there for testing - the sample value you provided.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I made one tweak to the if condition:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;let dateString = 1614470400000;
let newDate = addMonths(dateString,12);
console.log(newDate); // just to check

newDate = newDate.getTime()
console.log(newDate);

function addMonths(date, months) {
    var od = new Date(date);
  	var nd = new Date(date);    
    var od_m = od.getMonth();
    od.setMonth(od_m + months);
		if (od.getDate() != nd.getDate()) {
      od.setDate(0);
    }
    return od;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I run with the input you provided `1614470400000&lt;SPAN&gt;` I get `1646006400000` as the output for the new date. If you put those values in an epoch time converter you get:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Sunday, February 28, 2021 12:00:00 AM&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Monday, February 28, 2022 12:00:00 AM&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The if condition sets the date to the last day of the prior month in the case where the month in the new date has fewer days than the month in the original date.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;You can try it here in JsFiddle:&amp;nbsp;&lt;A href="https://jsfiddle.net/ktc8jgpn/1/" target="_blank"&gt;https://jsfiddle.net/ktc8jgpn/1/&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Open your console when you run it.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 19:41:04 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407725#M67</guid>
      <dc:creator>sam-g</dc:creator>
      <dc:date>2021-02-09T19:41:04Z</dc:date>
    </item>
    <item>
      <title>Re: Probably a newb question, but, date formats... help?</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407770#M68</link>
      <description>&lt;P&gt;EDIT: Thank you so much. Here is the finished and functional code. Much appreciated for all the help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;const hubspot = require('@hubspot/api-client');

exports.main = (event, callback) =&amp;gt; {
  callback(processEvent(event));
};

function processEvent(event) {
  const hubspotClient = new hubspot.Client({ apiKey: process.env.WhiteRabbit });

  let dealId = event.object.objectId;

  hubspotClient.crm.deals.basicApi
    .getById(dealId, ['test'])
    .then(results =&amp;gt; {
let dateString = results.body.properties['test'];
let newDate = addMonths(dateString,12);
console.log(newDate); 
  
    hubspotClient.crm.deals.basicApi
        .update(
          dealId,
          {
            properties: {
              ['test']: addMonths(dateString, 12)
}
          }
        )
    });
}
function addMonths(date, months) {
	var od = new Date(date);
	var nd = new Date(date);    
	var od_m = od.getMonth();
    od.setMonth(od_m + months);
		if (od.getDate() != nd.getDate()) {
      od.setDate(0);
    }
    return od;
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 21:53:17 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407770#M68</guid>
      <dc:creator>SaleProcessNerd</dc:creator>
      <dc:date>2021-02-09T21:53:17Z</dc:date>
    </item>
    <item>
      <title>Re: Probably a newb question, but, date formats... help?</title>
      <link>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407786#M69</link>
      <description>&lt;P&gt;I've been working on a similar project (I made a separate thread about that), and it taught me a lot about dates in Javascript and HubSpot. (Like you, I'm learning Javascript on the fly, here.) I still don't know enough to troubleshoot your code, but I was able to write this code that gets the job done:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;const hubspot = require('@hubspot/api-client');

exports.main = (event, callback) =&amp;gt; {
  callback(processEvent(event));
}

function processEvent(event) {
  const hubspotClient = new hubspot.Client({
    apiKey: process.env.WhiteRabbit
  });
  
  let dealId = event.object.objectId;
  hubspotClient.crm.deals.basicApi.getById(dealId, ["test"])
    .then(results =&amp;gt; {
      let dateString = new Date(results.body.properties.test);
    
dateString.setFullYear(dateString.getFullYear() + 1);
    
    hubspotClient.crm.deals.basicApi.update(
      dealId,
      {properties: {["test"]: dateString}}
    )
    
    })
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The top part is the same as yours. The important part is this bit in the middle:&amp;nbsp;dateString.setFullYear(dateString.getFullYear() + 1);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dateString.getFullYear() returns the year from dateString. dateString.setFullYear() updates the year of dateString. So I put getFullYear() + 1 inside of setFullYear(), and that did the trick.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does that help?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Feb 2021 22:00:04 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/Data-Hub/Probably-a-newb-question-but-I-need-help-with-date-formats-help/m-p/407786#M69</guid>
      <dc:creator>KyleJepson</dc:creator>
      <dc:date>2021-02-09T22:00:04Z</dc:date>
    </item>
  </channel>
</rss>

