how get this specific format of date in pre-request of postman - datetime

I have this in c#:
var date = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);
and the result is like this:
date = "Tue, 27 Dec 2022 13:30:35 GMT";
I want to have this result in pre-request of postman to pass this variable as date.
But this command doesn't give me the exact result:
var date = new Date();
//result: Tue Dec 27 2022 16:26:00 GMT+0100 (Central European Standard Time)
As I'm using this date variable for encryption, it's important to have it in the special format I have in c#.
Do you have any idea how can I have this result in postman?

To display time, you can use momentjs, that's already included in postman. The cons is it doesn't support timezone, so the code would be:
const moment = require('moment')
let datetime = moment().format("ddd, DD MMM YYYY HH:mm:ss ") + "GMT"
//Wed, 28 Dec 2022 08:08:36 GMT

Using reg expression in pre-request section
var date = new Date();
// Tue Dec 27 2022 12:10:39 GMT-0500 (Eastern Standard Time)
console.log(date);
let match = /(Sun|Mon|Tue|Wed|Thu|Fri|Sat)\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+(\d{1,2})\s+(\d{4})\s+(\d{2}|\d{1})\:(\d{2})\:(\d{2})\s([a-zA-Z]{3})/.exec(date);
// 0: "Tue Dec 27 2022 12:10:39 GMT"
// 1: "Tue"
// 2: "Dec"
// 3: "27"
// 4: "2022"
// 5: "12"
// 6: "10"
// 7: "39"
// 8: "GMT"
// newDate = "Tue, 27 Dec 2022 13:30:39 GMT";
newDate = `${match[1]}, ${match[3]} ${match[2]} ${match[4]} ${match[5]}:${match[6]}:${match[7]} ${match[8]}`
console.log(newDate);
Result in console
Tue Dec 27 2022 12:22:39 GMT-0500 (Eastern Standard Time)
Tue, 27 Dec 2022 12:22:39 GMT
Test string set in https://regex101.com/
Regular Expression
(Sun|Mon|Tue|Wed|Thu|Fri|Sat)\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+(\d{1,2})\s+(\d{4})\s+(\d{2}|\d{1})\:(\d{2})\:(\d{2})\s([a-zA-Z]{3})
In Reg Expression Visualization https://regexper.com/

Related

Luxon DateTime fromISO off by one hour versus moment.js

Trying to replace moment.js in my Angular application with Luxon to reduce bundle size.
I have come across a case where the two libraries produce a different output, and I am not sure why.
moment.js produces a date that is one hour ahead.
const activeToDateTimeString = '2014-08-06T13:07:04';
let foo1 = moment(activeToDateTimeString).utcOffset(-5, true);
let foo2 = DateTime.fromISO(activeToDateTimeString, {zone: 'America/New_York'}).setZone('America/New_York', { keepLocalTime: true });
let foo3 = DateTime.fromJSDate(new Date(activeToDateTimeString)).setZone('America/New_York', { keepLocalTime: true });
let foo4 = DateTime.fromISO(activeToDateTimeString).setZone('America/New_York', { keepLocalTime: true });
console.log(foo1.toDate());
console.log(foo2.toJSDate());
console.log(foo3.toJSDate());
console.log(foo4.toJSDate());
Output:
Wed Aug 06 2014 14:07:04 GMT-0400 (Eastern Daylight Time)
Wed Aug 06 2014 13:07:04 GMT-0400 (Eastern Daylight Time)
Wed Aug 06 2014 13:07:04 GMT-0400 (Eastern Daylight Time)
Wed Aug 06 2014 13:07:04 GMT-0400 (Eastern Daylight Time)
Why does moment.js produce a different output in this case?
let foo1 = moment(activeToDateTimeString).utcOffset(-4, true);
This would correct your code, but as you're moving to Luxon the daylight time changes won't effect you in the future.
Right now (19 mar 2020) New York is 4 hours behind UTC as from the 8th March 2020 it entered Eastern Daylight Time from Eastern Standard Time.
If New York was in Eastern Standard Time at the moment your code would output the same times.

Date comparison works locally, but not on heroku

In a Vuejs app, I currently have a button that is usable once a day. After it is clicked once, it is disabled until the following day. This works fine locally, but on heroku, the comparison does not seem to work.
Here is y date computed value in the vue component which returns true if the Act was created after 00:00 on the current date:
computed: {
...
actedToday() {
if (!!this.$store.getters.lastAct) {
let now = new Date();
console.log('this is now in actedToday computed: ', now);
let lastActDate = new Date(this.$store.getters.lastAct.created_at);
console.log('this is last act date in computed actedToday', this.$store.getters.lastAct.created_at);
console.log('was last Act today? ', lastActDate.getTime() > now.setHours(0,0,0,0));
return lastActDate.getTime() > now.setHours(0,0,0,0)
}
}
},
Here is what the console.log returns locally:
this is now in actedToday computed: Fri Oct 04 2019 15:20:24 GMT-0400 (Eastern Daylight Time)
ActButton.vue?0bb7:31 this is last act date in computed actedToday 2019-10-04T19:20:23.901Z
ActButton.vue?0bb7:32 was last Act today? true
And here is the analogous logs from the heroku app:
this is now in actedToday computed: Fri Oct 04 2019 15:21:18 GMT-0400 (Eastern Daylight Time)
ActButton.vue:31 this is last act date in computed actedToday 2019-10-04T03:30:22.266Z
ActButton.vue:32 was last Act today? false
In the first example, the comparison works as expected. In the second, although both dates are on the same day, the comparison returns false.
Your problem occurs due to different timezones. Your lastActDate is in Zulu-time (UTC, note the Z at the end) but your actedToday is in EDT. Using setHours(0,0,0,0) will give you the start of the day, but in the same timezone. Thus you have the following values:
//locally
now = Fri Oct 04 2019 15:20:24 GMT-0400 (Eastern Daylight Time)
now.setHours(0,0,0,0) = Fri Oct 04 2019 00:00:00 GMT-0400 (Eastern Daylight Time)
lastActDate = 2019-10-04T19:20:23.901Z = Fri Oct 04 2019 15:20:23.901 GMT-0400 (Eastern Daylight Time)
//heroku
now = Fri Oct 04 2019 15:21:18 GMT-0400 (Eastern Daylight Time)
now.setHours(0,0,0,0) = Fri Oct 04 2019 00:00:00 GMT-0400 (Eastern Daylight Time)
lastActDate = 2019-10-04T03:30:22.266Z = Fri Oct 03 2019 23:30:22.266 GMT-0400 (Eastern Daylight Time)
Thus heroku is right and your last action has not been "today". You should consider to use the same timezone in all of your dates.

Moment.js wierd translation

I'm using moment.js and getting something strange:
Input string:
'Wed, 30 Aug 2017 19:53:54 EST'
Want parse it using Moment.js:
moment('Wed, 30 Aug 2017 19:53:54 EST', 'ddd, DD MMM YYYY HH:mm:ss z');
getting object:
_d: Wed May 31 2017 23:59:59 GMT+0300 (FLE Daylight Time) {}
_f: "ddd, DD MMM YYYY HH:mm:ss z"
_i: "Wed, 30 Aug 2017 19:53:54 EST"
_isAMomentObject: true
_isUTC: false
_isValid: true
_i - it's input
_f - as I can understand - format
_d - it's date, result of parsing, WHY there 'May 31'?
Found and fixed problem. If someone faced with same problem, they could check my solution.
Datetime object mutate in controller:
let myDate = moment();
------------ skip ------------
let someObj = {
yesterday: myDate.subtract(1, 'days')
};
Because it's object, definitions like below doesn't helps:
const myDate = moment();
or
let myDate = moment();
Object.freeze(myDate);
only work solution for me is:
let myDate = moment();
------------ skip ------------
let someObj = {
yesterday: myDate.clone().subtract(1, 'days')
};

parse strange date format

I'm working on a Parser which Parses log files from a game so I can do analysis on auctions made within the game, however the date format that's being written by the logger seems to be causing problems as the format seems to be custom written for the logger, an example datetime stamp looks like: [Wed Nov 23 23:26:10 2016] I try to Parse it with:
func (r *AuctionReader) extractSaleInformation(line string) {
fmt.Println("Extracting information from: ", line)
// Format mask for output
layout := "DD-MM-YYYY hh:mm:ss"
// Replace the square brackets so we're just left with the date-time string
date := strings.TrimSpace(strings.Replace((strings.Split(line, "]")[0]), "[", "", -1))
fmt.Println(time.Parse(date, layout))
}
When I attempt to Parse the above date-time string I get the following error:
0001-01-01 00:00:00 +0000 UTC parsing time "DD-MM-YYYY hh:mm:ss" as "Wed Nov 23 23:26:10 2016": cannot parse "DD-MM-YYYY hh:mm:ss" as "Wed Nov "
How am I able to get the parser to recognise this seemingly custom format, I will be saving this data to Mongo so I don't want to store the auction time as a string as I want to query the timestamps individually.
Golang handle all date formatting in a unique way - it uses the reference time Mon Jan 2 15:04:05 MST 2006 (01/02 03:04:05PM '06 -0700) to show the pattern with which to format/parse a given time/string.
So, to read the format "Wed Nov 23 23:26:10 2016" you would put the reference date into that format: "Mon Jan 2 15:04:05 2006", and then do:
t, _ := time.Parse("Mon Jan 2 15:04:05 2006", "Wed Nov 23 23:26:10 2016")
Then, to output it in the given format, if you wanted the format DD-MM-YYYY hh:mm:ss, you would put the reference time into that format: 02-01-2006 15:04:05, and then do:
t.Format("02-01-2006 15:04:05")
https://play.golang.org/p/VO5413Z7-z
So basically, the main change is
// Format mask for output
layout := "DD-MM-YYYY hh:mm:ss"
should be
// Format mask for output
layout := "02-01-2006 15:04:05"
and
time.Parse(date, layout)
should be
time.Parse(layout, date)

how to Convert String to Date in flex?

Actually in my Flex Application DataGrid column getting one value like Sat Sep 1 00:00:00 GMT+0530 2012
how to convert this String as Date like 2012/09/01
i'm doing converting String Date but it giving null value...hear is my sample code
var startDS:String = saveDataGrid.selectedItem.startDate;
var sDate:Date=DateField.stringToDate(startDs,"YYYY/MM/DD");
Alert.show(sDate+"--===--"+startDS);//Alert giving null--===--Sat Sep 1 00:00:00 GMT+0530 2012
please help...
var startDS:String = "10022008";
var sDate:Date=DateField.stringToDate(startDS,"MMDDYYYY");
Alert.show(sDate+"--===--"+startDS);
You might also need to have a glance at http://livedocs.adobe.com/flex/3/html/help.html?content=formatters_4.html
output will be, "Thu Oct 2 00:00:00 GMT+0530 2008--===--10022008"

Resources