How to get current year in the Postman script - postman

I have a requirement to get current year in the Postman pre-req script.
I'm planning to get the current date, then convert the date to string and apply sub-string to get year value
I would like to know, is this the right way of doing it, or is there any pre-defined function available to do it?

I guess there is no pre-defined function to get year alone may be you can try like below,
const moment = require('moment');
pm.globals.set("timestamp", moment().format("MM/DD/YYYY"));
Then reference {{timestamp}} where ever you need it.
check the link for more details

To get year -
var moment = require('moment');
console.log("timestamp", moment().format("YYYY"));
or even without using moment library -
console.log(new Date());

Another way to get current year :
new Date().getFullYear();

var currentYear=new Date().getFullYear();
postman.setEnvironmentVariable("currentYear" , currentYear);
This worked for me.

Related

How to get the floor of the current time to an hour in postman

I am a new postman. I want to set a variable with the floor of the current time.
Example: current time is "2022-09-07T05:17:59" (UTC time) BUT I want to set a variable with the value "2022-09-07T00:00:00" (UTC time)
Does anyone help me? Thanks so much!
This isn't a very elegant solution but should work
var floor_timestamp = new Date()
floor_timestamp.setMinutes(0)
floor_timestamp.setSeconds(0)
floor_timestamp.setMilliseconds(0)
console.log(floor_timestamp.toISOString())
pm.globals.set("timestamp", floor_timestamp)

Previous day in Postman request

I have below code in postman's pre-request script --which gives me current date. I rather want yesterday's date (current_timestamp - 1 day).
var current_timestamp = new Date();
postman.setEnvironmentVariable("current_timestamp", current_timestamp.toISOString());
I searched doc & net but could not get the answer. Can someone please help me with reference to date functions --to get my desired result.
Thanks
You can use the momentjs module in Postman to get a date in any format you need.
In the Pre-Request Script, add this to get what you need without using native JS:
var moment = require('moment')
pm.environment.set("current_timestamp", moment().toISOString())
pm.environment.set("current_timestamp - 1 day", moment().subtract(1, 'day').toISOString())
This snippet will bring in the module and set the dates you require in the environment file.
For a non moment solution in plain JavaScript to just quickly go back 24hrs, you could do something like this:
var yesterday = (Date.now() - 86400000) // 24hrs in ms
pm.environment.set('yesterday', new Date(yesterday).toISOString())
Both solutions would give you the same outcome but I prefer to use moment as it's a built-in module that handles dates and times very well.

How can I get the current day in Robot Framework?

Im new to automated Testing.
Now I want to select the current weekday from a list.
I want to us following Keyword:
Selenium2Library.Select From List by Value ${day_of_the_week}
How can I get the Current day in Robot Framework? Is there a simple solution to my Problem?
The best would be a variable that gives me the current day in german Language.
I hope you can help me.
Using the Library DateTime
Which can be called by:
**** Settings ***
Library DateTime
You can use the Get Current Date keyword and assign it to a Var and specify the format. For example:
${CurrentDate} Get Current Date result_format=%d-%m-%Y
Log ${CurrentDate}
In your case you need to change the Result_format to retrieve the current day. This would be
${CurrentDay} Get Current Date result_format=%A
Log ${CurrentDay}
Which, when viewed in the log, would retrieve "Thursday"
The list of formats can be found here
Hope this helps you!
EDIT:
Due to the DateTime Library not supporting local names for the dates (Montag for Monday in German for instance) The Asker switched to the %w format to return 0-6 range. Then used an index to change the int into a string of the date!

Graphlab Date Manipulaiton

I have a dataset that I am trying to manipulate in GraphLab. I want to convert a UNIX Epoch timestamp from the input file (converted to an SFrame) into a human readable format so I can do analysis based on hour of day and day of week.
time_array is the column/feature of the SFrame sf representing the timestamp, I have broken out just the EPOCH time to simplify things. I know how to convert the time of one row, but I want a vector operation. Here is what I have for one row.
time_array = sf['timestamp']
datetime.datetime.fromtimestamp(time_array[0]).strftime('%Y-%m-%d %H')
You can also get parts of the time from the timestamp to create another column, by which to filter (e.g., get the hour):
sf['hour'] = [x.strftime('%H')for x in sf['timestamp']]
So after staring at this for awhile and then posting the question it came to me, hopefully someone else can benefit as well. Use the .apply method with the datetime.datetime() function
sf['date_string'] = sf['timestamp'].apply(lambda x: datetime.datetime.fromtimestamp(x).strftime('%Y-%m-%d %H'))
you can also use the split_datetime API to split the timestamp to multiple columns:
split_datetime('timestamp',limit=['hour','minute'])

How to find current date of computer in ROS?

I'm trying to calculate difference of dates based on the current computer date, in ROS. Which function can I use to do that? Or atleast get the current date of computer.
Thanks in advance
You should try Google next time, but here:
ros::Time::now();
ros::Duration difference = ros::Time::now() - previous_time;
http://wiki.ros.org/roscpp/Overview/Time
EDIT:
To get a text string, you have to convert it: https://code.ros.org/trac/ros/ticket/2030
boost::posix_time thistime = from_time_t(difference);
Once you have it converted to boost::posix_time, you can:
std::string to_simple_string(thistime);
Which will spit it out like: "2002-Jan-01 10:00:01.123456789"
You can also see what thistime.date(); gives you, it looks like it might be simpler: http://www.boost.org/doc/libs/1_31_0/libs/date_time/doc/class_date.html