``` function formatDate(date) { var formatedD...
# suitescript
m
Copy code
function formatDate(date) {
    var formatedDate = format.format({
      value: new Date(date),
      type: format.Type.DATE,
    });
    return formatedDate.toString();
  }

  function formatDate2(date) {
    var formatedDate = format.format({
      value: new Date(date),
      type: format.Type.DATETIME,
    });
    return formatedDate.toString().split(" ")[0];
  }

  function execute() {
    try {
      log.audit("formatDate", formatDate("2022-05-18T04:57:06.798Z"));
      log.audit("formatDate2", formatDate2("2022-05-18T04:57:06.798Z"));
    } catch (err) {
      log.audit("Error", err);
    }
  }
This script prints
Copy code
formatDate  17/5/2022	
formatDate2 18/5/2022
Any idea why? Why
format.Type.DATE
gives a different result than
format.Type.DATETIME
?
c
It'll be timezones, and the user preference. You can specify the timezone option in your call to use UTC (or whatever suits you), rather than the user default: https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_4407050795.html
m
@CD Thanks, but this is not correct. Both functions doesn't specify timezones, so the behavior should be the same. Also, I tried setting the timezone, and still they gave different results.
b
it should be more obvious if you compare
"2022-05-18T06:59:59.999Z"
to
"2022-05-18T07:00:00.000Z"
m
@battk I just tried both (while specifying the timezone for both functions) The problem still happened with this
"2022-05-18T06:59:59.999Z"
but not this
"2022-05-18T07:00:00.000Z"
So why is that? I don't understand
b
as far as i know, all servers run in pacific timezone
which right now is a -7 offset
m
But I specified the timezone
b
doesnt matter for Date formatting
m
I don't understand
b
a netsuite date doesnt have a time component
netsuite accomplishes that by ignoring the time components
m
yes, but ignoring it should still return
18
in this case... right?
b
the server's timezone is pacific
Copy code
"2022-05-18T04:57:06.798Z"
is equivalent to
Copy code
"2022-05-17T21:57:06.798-7:00"
m
So basically the type
DATETIME
respects the timezone parameter, but
DATE
doesn't... right?
b
yes
m
Ok, thanks... I just think that this is not intuitive and not in the docs 😢
b
if it help, the format function looks like
Copy code
function doFormat(options, type) {
  var value,
    timezone = null,
    undef = undefined;

  if (type !== undef) {
    value = options;
  } else if (options !== undef && options !== null) {
    value = options.value;
    type = options.type;
    timezone = options.timezone || null;
  }

  utilityFunctions.checkArgs([value, type], ["value", "type"], "format");

  switch (type) {
    case fieldTypeConstants.Type.DATE:
    case fieldTypeConstants.Type.TIME:
    case fieldTypeConstants.Type.TIMETRACK:
    case fieldTypeConstants.Type.TIMEOFDAY:
    case fieldTypeConstants.Type.INTEGER:
    case fieldTypeConstants.Type.POSINTEGER:
    case fieldTypeConstants.Type.PERCENT:
    case fieldTypeConstants.Type.FLOAT:
    case fieldTypeConstants.Type.POSFLOAT:
    case fieldTypeConstants.Type.NONNEGFLOAT:
    case fieldTypeConstants.Type.POSCURRENCY:
    case fieldTypeConstants.Type.NONNEGCURRENCY:
    case fieldTypeConstants.Type.CURRENCY:
    case fieldTypeConstants.Type.CURRENCY2:
    case fieldTypeConstants.Type.CHECKBOX:
    case fieldTypeConstants.Type.PHONE:
    case fieldTypeConstants.Type.FULLPHONE:
    case fieldTypeConstants.Type.MMYYDATE:
    case fieldTypeConstants.Type.RATE:
    case fieldTypeConstants.Type.RATEHIGHPRECISION:
      return formatter.format(value, type);

    case fieldTypeConstants.Type.DATETIME:
    case fieldTypeConstants.Type.DATETIMETZ:
      return dateTimeZone.format(value, timezone);

    default:
      return value;
  }
}
only 2 types use the timezone
m
got it, thanks 🙏