I want to compare if a given date is in the past or future.
The given date is coming in from a string in yyyy-mm-dd format.
I tried to "even out" the today and compensating the timezone in the given date date but I am sure there must be a better way to this??
var today = new Date();
console.log("TODAY: " + today); // Mon Apr 28 2014 14:46:41 GMT+0200 (CEST)
var todayYear = today.getFullYear();
var todayMonth = today.getMonth();
todayMonth = parseInt(todayMonth, 10) + 1;
var todayDay = today.getDate();
var todayFormatted = todayYear + "-" + todayMonth + "-" + todayDay;
today = new Date(todayFormatted);
console.log("TODAY: " + today); // Mon Apr 28 2014 00:00:00 GMT+0200 (CEST)
var testDate = new Date("2014-04-28");
console.log("TEST: " + testDate); // Mon Apr 28 2014 02:00:00 GMT+0200 (CEST)
testDate.setHours(00);
console.log("TEST: " + testDate);
// check if test is in the past
(testDate < today) ? console.log('test is in the past') : console.log('test is NOT in the past');
You can compare the "Unix times" mathematically:
var today = new Date();
console.log(testDate.getTime() < today.getTime()
? 'test is in the past'
: 'test is NOT in the past');
Related
This question already has answers here:
How to format current time using a yyyyMMddHHmmss format?
(6 answers)
Parsing date/time strings which are not 'standard' formats
(4 answers)
Closed 4 months ago.
I have a date string Oct 10 2022 and I want to convert this to a time object. I have tried with time.Parse, but it always returns 0001-01-01 00:00:00 +0000 UTC
date := "Oct 10 2022"
output, _ := time.Parse(time.ANSIC, date)
fmt.Println(output)
How do I get a time object from the above string?
for cast your favorite time string to time.ANSIC, you must do it like below
date = "Mon Oct 10 15:04:05 2022"
output, _ := time.Parse(time.ANSIC, date)
fmt.Println(output)
other time package constants for cast like below:
Layout = "01/02 03:04:05PM '06 -0700" // The reference time, in numerical order.
ANSIC = "Mon Jan 2 15:04:05 2006"
UnixDate = "Mon Jan 2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
Stamp = "Jan 2 15:04:05"
StampMilli = "Jan 2 15:04:05.000"
StampMicro = "Jan 2 15:04:05.000000"
StampNano = "Jan 2 15:04:05.000000000"
Please how do i get "today's date function" into a Column on Emeditor? Similar to Excel
You can use a JavaScript macro like this:
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();
document.write( mm + '/' + dd + '/' + yyyy );
Cannot use 'bgcolor' in local scope
my code :
//#version=4
//strategy("try 2", overlay=true)
study("Astrolog 2", "Astrolog 2", overlay=true)
yearStart = 2015
yearEnd = 2021
for counter = yearStart to yearEnd [1]
i_startTime = input(defval = timestamp("23 Aug 2020 00:00 +0000"), title = "Start Time", type = input.time)
i_endTime = input(defval = timestamp("22 Sep 2020 23:59 +0000"), title = "End Time", type = input.time)
i_length = input(defval = 20, title = "Length", type = input.integer)
inDateRange = time >= i_startTime and time <= i_endTime
bgcolor(inDateRange ? color.green : na, 50)
break
I want every 23 Aug - 22 sept have background color
yearStart = input(2015)
monthStart = input(8)
dayStart = input(23)
yearEnd = input(2021)
monthEnd = input(9)
dayEnd = input(22)
inDayMonthRange = time >= timestamp(year, monthStart, dayStart, 0, 0) and time <= timestamp(year, monthEnd, dayEnd, 0, 0)
inYearRange = year >= yearStart and year <= yearEnd
inRange = inDayMonthRange and inYearRange
bgcolor(inRange ? color.green : na, 50)
You don't need to use a loop, pine's execution model executes the script progressively through each historical bar.
year returns each bar's year portion of the timestamp. So as the script progresses through each historical bar, you can test separately if we are in the day/month range, and then also test if it is in your range of years.
I would like to get current time value. I found this answer which works for me but don't know why format method take 20060102150405 value? Not like yyyyMMdd hhmmss.
Go's time formatting unique and different than what you would do in other languages. Instead of having a conventional format to print the date, Go uses the reference date 20060102150405 which seems meaningless but actually has a reason, as it's 1 2 3 4 5 6 in the Posix date command:
Mon Jan 2 15:04:05 -0700 MST 2006
0 1 2 3 4 5 6
The timezone is 7 but that sits in the middle, so in the end the format resembles 1 2 3 4 5 7 6.
This online converter is handy, if you are transitioning from the strftime format.
Interesting historical reference: https://github.com/golang/go/issues/444
The time package provides handy constants as well:
const (
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
// Handy time stamps.
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
)
You can use them like this:
t := time.Now()
fmt.Println(t.Format(time.ANSIC))
See https://golang.org/pkg/time/#pkg-constants It is the time "01/02 03:04:05PM '06 -0700" Because each component has a different number (1, 2, 3, etc.), it can determine from the numbers what components you want.
20060102150405 is a date and time format 2006/01/02 15:04:05
package main
import (
"fmt"
"time"
)
func main() {
date1 := time.Now().Format("2006/01/02 15:04")
fmt.Println(date1)//2009/11/10 23:00
date2 := time.Now().Format("20060102150405")
fmt.Println(date2)//20091110230000
}
https://play.golang.org/p/kIfNRQ50JP
I have StartDate (for instance 2011-01-01) and EndDate (for instance 2011-12-31).
All my data are placed in this range.
In test plan I need to generate random interval with fixed duration
(for 4 cases: 1, 3, 7 days and 1 month long)
which are placed in this date range [2011-01-01; 2011-12-31].
Each of these cases must have defined weights.
How should I do to get fixed interval with random Start Date and random End Date (and put these random dates into 2 variables)?
I have found this variant of java-script
The initial script is here:
var startDate = new Date();
startDate.setDate(1);
startDate.setMonth(0);
startDate.setYear(1991);
var startDateTime = startDate.getTime();
var endDate = new Date();
endDate.setDate(31);
endDate.setMonth(11);
endDate.setYear(2003);
var endDateTime = endDate.getTime();
var randomDate = new Date();
var randomDateTime = startDateTime+Math.random()*(endDateTime-startDateTime);
randomDate.setTime(randomDateTime);
var rndDate = randomDate.getDate();
var rndMonth = randomDate.getMonth() + 1;
var rndYear = randomDate.getFullYear();
if (rndDate.toString().length == 1)
rndDate = "0" + rndDate;
if (rndMonth.toString().length == 1)
rndMonth = "0" + rndMonth;
rndDate + "/" + rndMonth + "/" + rndYear;
But I need generate random start date of the fixed interval (which I called ${RandomStartDate}) and then to add the length of the interval to get the end date of it (this date I called ${RandomEndDate}).
Then I have change the script for 1 day long interval (1 day is 86400 seconds):
var startDate = new Date();
startDate.setDate(01);
startDate.setMonth(01);
startDate.setYear(2011);
var startDateTime = startDate.getTime();
var endDate = new Date();
endDate.setDate(31);
endDate.setMonth(12);
endDate.setYear(2011);
var endDateTime = endDate.getTime();
var randomSDate = new Date();
var randomSDateTime = startDateTime+Math.random()*((endDateTime - 86400) -startDateTime );
randomSDate.setTime(randomSDateTime);
var randomEDate = new Date();
var randomEDateTime = (randomSDateTime + 86400); //add 1 day long interval (86400 s)
randomEDate.setTime(randomEDateTime); //convert number format to string format of date
var rndSDate = randomSDate.getDate();
var rndSMonth = randomSDate.getMonth()+1 ;
var rndSYear = randomSDate.getFullYear();
var rndEDate = randomEDate.getDate();
var rndEMonth = randomEDate.getMonth()+1 ;
var rndEYear = randomEDate.getFullYear();
if (rndSDate.toString().length == 1)
rndSDate = "0" + rndSDate;
if (rndSMonth.toString().length == 1)
rndSMonth = "0" + rndSMonth;
if (rndEDate.toString().length == 1)
rndEDate = "0" + rndEDate;
if (rndEMonth.toString().length == 1)
rndEMonth = "0" + rndEMonth;
var RandomStartDate = rndSYear + "-" + rndSMonth + "-" + rndSDate;
vars.put ("RandomStartDate", RandomStartDate);
var RandomEndDate = rndEYear + "-" + rndEMonth + "-" + rndEDate;
vars.put ("RandomEndDate", RandomEndDate);
But this script generates RandomEndDate which is equal to RandomStartDate.
If I generate RandomStartDate separately (without the parts of code which are connected with RandomEndDate) the script works good.
Could you help me, please? What is wrong?
You can do it in Beanshell with much less amount of code:
import java.text.SimpleDateFormat;
import java.util.Calendar;
calendar = Calendar.getInstance();
calendar.set(2011, 0, 1);
startTime = calendar.getTimeInMillis();
calendar.set(2012, 11, 31);
endTime = calendar.getTimeInMillis();
randomTime1 = startTime + (long)(Math.random()*(endTime-startTime));
randomTime2 = randomTime1 + (long)(Math.random()*(endTime - randomTime1)+86400000);
formatter = new SimpleDateFormat("yyyy-MM-dd");
calendar.setTimeInMillis(randomTime1);
vars.put("start", formatter.format(calendar.getTime()));
calendar.setTimeInMillis(randomTime2);
vars.put("end", formatter.format(calendar.getTime()));
For situations like this I would opt to put the complexity outside of jMeter, and create a perl script to generate 100 pairs of dates according to your requirements and read them into jMeter variables using CSV Data Set Config.
I have found the solution!
This is the correct script, which allow to model the random interval with fixed length (1 day long interval) and get 2 variables fot it start and end - ${RandomStartDate} and ${RandomEndDate}:
var startDate = new Date();
startDate.setDate(01);
startDate.setMonth(01);
startDate.setYear(2011);
var startDateTime = startDate.getTime();
var endDate = new Date();
endDate.setDate(31);
endDate.setMonth(12);
endDate.setYear(2011);
var endDateTime = endDate.getTime();
var randomSDate = new Date();
var randomSDateTime = startDateTime+Math.random()*((endDateTime - 86400000) -startDateTime );
randomSDate.setTime(randomSDateTime);
var randomEDate = new Date();
var randomEDateTime = (randomSDateTime + 86400000); //add 1 day long interval (86400000 ms)
randomEDate.setTime(randomEDateTime); //convert number format to string format of date
var rndSDate = randomSDate.getDate();
var rndSMonth = randomSDate.getMonth()+1 ;
var rndSYear = randomSDate.getFullYear();
var rndEDate = randomEDate.getDate();
var rndEMonth = randomEDate.getMonth()+1 ;
var rndEYear = randomEDate.getFullYear();
if (rndSDate.toString().length == 1)
rndSDate = "0" + rndSDate;
if (rndSMonth.toString().length == 1)
rndSMonth = "0" + rndSMonth;
if (rndEDate.toString().length == 1)
rndEDate = "0" + rndEDate;
if (rndEMonth.toString().length == 1)
rndEMonth = "0" + rndEMonth;
var RandomStartDate = rndSYear + "-" + rndSMonth + "-" + rndSDate;
vars.put ("RandomStartDate", RandomStartDate);
var RandomEndDate = rndEYear + "-" + rndEMonth + "-" + rndEDate;
vars.put ("RandomEndDate", RandomEndDate);
My mistake was I thought that the time is in seconds, but it is in milliseconds!