0

I want to delete the records from Data Extension based on Date Field in my DE i.e. I want to delete record which were created date before 30 days.But I didn't find date manipulation function in SSJS like dateadd() in ampscript

4

You can do date math like you would in regular JS. I typically use a simple function to format the date:

<script type="text/javascript" runat="server">

Platform.Load("core", "1.1.1");

var today = new Date();
var prevDate = new Date();
prevDate.setDate(today.getDate()-30);
Write("<br>today: " + today);
Write("<br>prevDate: " + prevDate);
Write("<br>formatted prevDate: " + getTimestamp(prevDate));


function getTimestamp(dt) {

  var h = dt.getHours();
  var m = dt.getMinutes();
  var s = dt.getSeconds();
  if(h<10) h = '0'+h;
  if(m<10) m = '0'+m;
  if(s<10) s = '0'+s;

  var ts = (dt.getMonth()+1) + '/' + dt.getDate() + '/' + dt.getFullYear() + ' ' + h + ':' + m + ":" + s;

  return ts;
}


</script>

Output

today: Tue, 25 Oct 2016 07:35:44 GMT-06:00
prevDate: Sun, 25 Sep 2016 07:35:44 GMT-06:00
formatted prevDate: 9/25/2016 07:35:44 
  • thanks just 1 more question... where will the output of write() come? on Microsite – Prateek Nara Oct 27 '16 at 11:51
  • It's just for output from within the script block. It'll show wherever it's executed. – Adam Spriggs Oct 27 '16 at 11:55
  • when i paste above code in interaction>script and then run that script where will the output come? – Prateek Nara Oct 27 '16 at 12:48
  • Script activities don't have output, but you can write to a data extension for logging. – Adam Spriggs Oct 27 '16 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.