Converting a date to a string - SuiteScript 2.0 - netsuite

Goal: Convert JS Date Object to a String representation in the format of "11/2/2017" in a NetSuite SuiteScript 2.0 scheduled script.
I have a date object that I need to use for 2 purposes. In one, I am going to use it for comparisons (so I want the actual date object). The other is I want it to be the name of a Custom Record, ie a string value.
I am doing this in NetSuite SuiteScript 2.0 (Javascript) in a Scheduled Script. The toString() of the date right now is: "2017-11-02T07:00:00.000Z". The format I want to end up with for the name is 11/2/2017.
When I test toLocaleDateString() in a browser test app, I get 11/2/2017 - the exact format I want. However, when I sue this same thing in SuiteScript 2.0 I get "November 2, 2017". I know there is a difference between client/server but this was frustrating.
I tried the format.parse() function as NetSuite's documentation claims that this is the equivalent to the 1.0 nlapiDateToString() function. This did not work.
Besides writing my own function (which I am tempted to do), does anyone know how to accomplish this goal?

To switch over to that format you would not use format.parse, you would use format.format. Here is a simple example of converting a date object to that string format.
require(['N/format'],function(format){
function formatDate(testDate){
log.debug('testDate: '+testDate);
var responseDate=format.format({value:testDate,type:format.Type.DATE});
log.debug('responseDate: '+responseDate);
}
var testDate=new Date();
formatDate(testDate);
});

I'm going to suggest using the momentJS library for all of your SuiteScript date manipulation needs. It works well as a SuiteScript 2.0 module and you can format dates easily:
var now = new Date();
var formattedDate = moment(now).format('M/D/YYYY');

Use format.parse module of suitescript 2.0
var myDateString= "04/26/2020";
var parseDate = format.parse({
value: myDateString,
type: format.Type.DATE
});

Related

Dayjs to JS Date for Mongo

I need to parse a user supplied date in NodeJS. I found answers stating that using built in Date constructor/parse method is not reliable and I should use some library like Moment. I was confused that there are Moment and MomentJS libraries and found that there is a tiny library DaysJS having the same interface. Parsing works fine but I cannot find a way how to get the JS Date object that I need to pass to Mongo. Is there any aother way than extract the unix milliseconds and pass it to Date constructor?
When I pass daysjs instance to Mongo NodeJS driver, it fails:
const day = dayjs('2020-01-02', 'YYYY-MM-DD');
2020-06-07 10:42:33:093 [error]: Request failedThe dollar ($) prefixed field '$L' in 'info.date.$L' is not valid for storage.
This seems to work:
let publishDate = new Date(1000 * dayjs().unix());
Is it the correct way of daysjs with Mongo?
The error you got is because you tried to pass a DayJS object to mongodb, which it doesn't support.
You can pass a javascript Date object to mongodb. In order to convert a DayJS object to plain javascript Date object you can use .toDate() on the DayJS object
dayjs('2020-01-02', 'YYYY-MM-DD').toDate()

netsuite suitescript 2.0 export(csv)

Is there a way to export search results using suitescript 2.0 in the same way when exporting from the Search page using Export(CSV). Netsuite Answers says that this can be done by building a CSV file, I would like to know if I can run the Export(CSV) as is. I need to do this because I have many searches that I need to run weekly which have to be downloaded to Excel and I would like to have a script do this instead of manually selecting each one.
Use the N/task.SearchTask API.
The inbuilt solution provided by Netsuite is to schedule the saved search to send email with the saved search results to be send as attachment in CSV format .
Alternatively, you can also find third party library to convert JSON to CSV and convert the saved search result to JSON format you want to be in CSV
Really quick code of a scheduled script that puts the results of a saved search into an exiting file.
Ref: SuiteScript 2.0 API page 792
/**
*#NApiVersion 2.x
*#NScriptType ScheduledScript
*/
define(['N/task','N/log'],
function(task)
{
function execute(context)
{
//create search task
var myTask = task.create({
taskType: task.TaskType.SEARCH
});
myTask.savedSearchId = 4222;
myTask.fileId = 14581313;
var myTaskId = myTask.submit();
log.audit({title:"Task submitted.",
details:"Put results of savedSearchId:4222 in csv file InternalID:14581313"});
}
return {execute: execute
}
});
I then check if the file is new enough (the script didn't fail) and download and process it.

Decoded Timestamp is not the same date as original DateTime value

We have a .NET application that defines a DateTime in the schema like so:
[ProtoMember(20)]public DateTime? Birthdate;
The application is able to serialize the data using protobuf-net and later upon deserialization the date is retrieved accurately as expected.
I am now trying to deserialize the same buffer in our node.js application using protobuf.js. I've defined that data point in the .proto file like so:
google.protobuf.Timestamp Birthdate = 20;
Upon decoding it the resulting Birthdate is not the same date as the original data. For example, when the date is originally 10/10/1976, the deserialized date is:
"Birthdate": {
"seconds": "4948"
}
When creating a JavaScript Date from that (new Date(4948 * 1000)), the result is 1/1/1970. What is going wrong here?
This comes down to history. protobuf-net had support for DateTime a long time before there was a well-defined Timestamp in the shared libraries. So: it invented something that would allow round-trip between protobuf-net and itself. Many years later, along comes Timestamp, and ... put simply, it uses a different storage layout. The good news is: protobuf-net can talk that dialect, but because we couldn't break existing code, it is "opt in".
If you use a Timestamp in a .proto, you can see that protobuf-net generates, for that .proto:
[global::ProtoBuf.ProtoMember(20, DataFormat = global::ProtoBuf.DataFormat.WellKnown)]
public global::System.DateTime? Birthdate { get; set; }
or more simply, to match your existing code:
[ProtoMember(20,DataFormat=DataFormat.WellKnown)]public DateTime? Birthdate;
With that in place - it should be using the same data layout tand you should get the same values. This is the recommended option if you need to exchange data between platforms. However, note that this is a change to your existing layout. If you need tips on migrating without breaking existing usage, let me know - it is possible (the short version would be "leave field 20 as the old style; add a new property that acts similarly and uses the new format - only serialize the new one, but allow the old one to deserialize").

how to display local date/time in browser?

I am coding an nodejs express application. The dates are stored in database using the UTC date/time. I'd like to display the date/time in the user's local time zone. how could I implement this?
Thank you in advance.
Richard Xu
In your template you could do
<span data-date-utc="Wed, 25 Apr 2018 18:16:13 UTC"></span>
Then use js to render it in the users local time (I'm using jQuery here to extract the date string, then render it back to the page)
$('span').text(new Date($('span').attr('data-date-utc')).toLocaleString());
You can use any of the Date Object methods in place of toLocaleString to render your output however you want
Use the moment npm package and for more information refer to the momentjs documentation.
var utcDate = moment.utc("2018-04-26 10:00:00");
var localDate = moment(utcDate).local();
Now you can use the below function to format the date accordingly.
localDate.format("YYYY-MM-DD HH:mm:ss");

How to pass notedate for User Notes in Netsuite using RESTlet?

I could create and update User Notes in Netsuite for the customer.
The following data is working fine.
$columns_field= array("title","direction","notedate"); #_scheduled
$columns_values=array("Sample User Notes","2","4/30/2016");
$datastring = array('recordtype' => 'note', 'id' => '104','columnname' =>$columns_field,'columnvalues'=>$columns_values,'gu_action'=>'update');
But when i add time for notedate , it will not work. I want to pass date and time. i have tried these formats.
Like
4/30/2016 05:06 am
4/30/2016 05:06 AM
4/30/2016 14:32
I am having RESTlet file in the following link:
https://gist.githubusercontent.com/ganeshprabhus/68a9e5b81e53436bb1d684f857a6c31f/raw/67fe03895f1c31d65c1f283dd51584af45d27c59/NS_Script_2016.2004.
My reference link is :
https://system.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2015_2/schema/record/note.html
Additional Requirement :
If possible, Please post the reference links.
For the Note record date and time are separate fields. Their script ids are "notedate" and "time" respectively.
So if you have a real Date:
note.setFieldValue("notedate", nlapiDateToString(datetime)); //'date' format i s the default
note.setFieldValue("time" , nlapiDateToString(datetime, 'timeofday'));
Try "2015-03-25T12:00:00" for your date string. That's a format that the Javascript Date object will understand, which is what you're ultimately working with in NetSuite (actually it's Rhino + Java, but close enough)
BTW, the T above stands for UTC time, so make sure to keep that in mind.

Resources