cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

Javascript service

i_abidi
10-Marble

Javascript service

Does anyone know how to convert timestamp to Datetime in Javascript please? thank you so much? 

This is my service that returns dates but in MS i need to bind its output to an SQL query who uses datetime as inputs : 

var currentDate=new Date(Date.now());
var currentWeekDay=currentDate.getDay();
var currentHour=currentDate.getHours();
var currentMonthDay=currentDate.getDate();
var currentMonth=currentDate.getMonth();
var currentYear=currentDate.getFullYear();
var SelectedButton ;
var myStartDate;
var myEndDate ;
var i=currentWeekDay;
var j=currentMonthDay;
var newEntry=new Object ();
if (SelectedButton=="Yesterday"){
myStartDate=currentDate;
myStartDate.setDate((currentDate.getDate())-1);
myStartDate.setHours(06);
myEndDate=new Date(Date.now());
myEndDate.setHours(06);

}
if (SelectedButton=="Week To Date"){
myStartDate = currentDate;
myStartDate.setHours(06);
if(j<i) { myStartDay.setDate(30-(7-i+j));
myStartMonth.setMonth=currentMonth-1;
myStartDate.setHours(06);} else {
myStartDate=currentDate;
myStartDate.setDate(j+1-i);
myStartDate.setHours(06);
}
myEndDate=new Date(Date.now());
myEndDate.setHours(06);
}


newEntry.StartDate= myStartDate;
newEntry.EndDate = myEndDate;

var params = {
infoTableName : "InfoTable",
dataShapeName : "StartEndDate"
};
var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

result.AddRow(newEntry);

 

ACCEPTED SOLUTION

Accepted Solutions

If you want to export Javascript Date objects to Unix timestamp you can do yourDateObject.getTime().

 

If you want to format Javascript Date objects to a String based DateTime you can use dateFormat(yourDateObject,"desired_date_format") or dateFormatISO(yourDateObject)

 

Thouse are the options on server side to convert Dates to different "object formats".

 

But always remember that at server side, when you use dateFormat it will be on Server TimeZone.

View solution in original post

6 REPLIES 6

If you want to export Javascript Date objects to Unix timestamp you can do yourDateObject.getTime().

 

If you want to format Javascript Date objects to a String based DateTime you can use dateFormat(yourDateObject,"desired_date_format") or dateFormatISO(yourDateObject)

 

Thouse are the options on server side to convert Dates to different "object formats".

 

But always remember that at server side, when you use dateFormat it will be on Server TimeZone.

you got a point now that i read it again :p , i'll explain : the javascript i wrote is supposed to generate a start and an end time based ona  string input. i want to use my output to input an SQL Query that uses DATETIME parameters .

When i run my JAVASCRIPT on test mode it generates results in ms as my datashape of my result info table has string parameters, but when i change the datashape to something with datetime parameters it generates nothing.


you got a point now that i read it again :p , i'll explain : the javascript i wrote is supposed to generate a start and an end time based ona  string input. i want to use my output to input an SQL Query that uses DATETIME parameters .

When i run my JAVASCRIPT on test mode it generates results in ms as my datashape of my result info table has string parameters, but when i change the datashape to something with datetime parameters it generates nothing.

 


the problem is i dont want a string because it wouldn't be readable by my SQL query input that i'll use later.

I want my javascript results to be DATETIME type so i basically need conversion in the opposite direction as from string to datetime type.

I did an integration with an Access database, and that's my code snippet  to pass the correct "DateTime" object for access database:

Params

var tz = me.GetTimeZone_wupGenericTS();
var tzF = Resources["TimeZoneFunctions"];
var year = 	tzF.GetYear({	dateTime: dateTime, timeZone: tz});
var month = tzF.GetMonth({	dateTime: dateTime, timeZone: tz});
var day = 	tzF.GetDay({	dateTime: dateTime, timeZone: tz});
var hour = 	tzF.GetHour({	dateTime: dateTime, timeZone: tz});
var minute = tzF.GetMinute({dateTime: dateTime, timeZone: tz});
var second = tzF.GetSecond({dateTime: dateTime, timeZone: tz});

var result = "(DateSerial("+year+","+month+","+day+")+TimeSerial("+hour+","+minute+","+second+"))";

Our solution it's TimeZone aware and we need special functions at server side to get the correct TimeZone, but you can use the standard getHour, getDay javascript functions to get a similar output.

Thank you so much Carles,

your function dateFormat(yourDateObject,"desired_date_format") worked

Announcements


Top Tags