Previous day in Postman request - postman

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.

Related

SwiftUI DatePicker and Date wrong timezone, not using the local one

I am implementing a DatePicker in SwiftUI.
#State var dateTime = Date()
DatePicker("Date and time, selection: $dateTime)
However, when I select a date & time there and try to print the result I get a different time.
print(self.dateTime.description(with: .current))
print(self.dateTime)
The idea is that if I select 10:28 PM, the first line with .current returns the selected value, but as a String and the second one returns a completely different value, usually with some hours added which I think it takes by default UTC+0 or something, I am now in GMT+3.
I think I have read a lot of answers in the past hour but nothing exactly about this. Any idea is much appreciated.

How do I get current ISO8601 timestamp in Postman Pre-Query Script and Body?

Using Postman and the following code in pre-query script
const moment = require('moment');
pm.globals.set("timestamp", moment().format("YYYY-MM-DDTHH:MM:SSZ"));
I get as a response
Request signature is too far in the past and has expired. Timestamp date: 2019-11-30T10:11:10+00:00
In body I am using {{timestamp}}.
I need timestamp in ISO8601 format.
If I use
{{$timestamp}}
it returns a Linux date of 1575110444 which is correct today at 10:41
It seems there is now an $isoTimestamp variable available out of the box
Check this out postman inbuilt variables
{{$timestamp}} is postman's inbuilt dynamic variables which will return always unix timestamp.
what you can do is rename the variable you setting -
const moment = require('moment');
pm.globals.set("timestamp1", moment().format("YYYY-MM-DDTHH:mm:ssZ"));
and use it {{timestamp1}}
Notice the $ sign in the inbuilt variable.
check this thread for more info
Update
From June 2020 onwards there is inbuilt variable provided for this as $isoTimestamp- for more details refer the documentation - https://learning.postman.com/docs/writing-scripts/script-references/variables-list/#common

How to get current year in the Postman script

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.

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!

Google Visualization Datatable: DateFormatter is not working

I'm trying to show 12 hours format on Google Chart timeline.
This is the example from Google with arrayToDataTable method. It is working fine and this is the result I really want in 12hrs format with am & pm.
Code Pen link 1: http://codepen.io/Thein/pen/mAOrow
This is how I populated dataTable using google.visualization.DataTable().
Additionally, I formatted -
var formatter_12h = new google.visualization.DateFormat({ pattern: "d/MM/yy h:m:s aa" });
formatter_12h.format(dataTable, 1);
formatter_12h.format(dataTable, 2);
It always comes up with 24 hours format in tooltip.
Code Pen link 2: http://codepen.io/Thein/pen/KgNvJA
I tried to play with Culture value in in web.config as follow but it is still showing 24h format.
<globalization enableClientBasedCulture="false" uiCulture="en-AU" culture="en-AU" />
EDIT: It seems that Snippet 2 code is working with 12 hours format on
Apahache server and It is showing 24 hours format on IIS server.
Thanks to WhiteHat and his solution. I have fixed the Timeline chart as follow:
Code Pen link 3: http://codepen.io/Thein/pen/LRbdKv
HhAxis is quite messy though. It is so much cleaner in Code Pen link 1(http://codepen.io/Thein/pen/mAOrow). Please post your answer if you can get the result exactly like Code Pen link 1 with dataTable, I will accept your answer.