i know it might be impossible to do what i need but i'm searching for a way to ensure that a user input date in the right format.
i have written a piece of code that can validate dates who the DD (day) part is between 13-31 but i'm struggling with the dates that has DD between 1-12
for ex:
i can validate this date 23/06/2016 and fix it to be 06/23/2016
but i can't ensure the date 12 of June is entered correctly.
if someone will input 12/06/2016 it's by definition a valid date but not the one i need (06/12/2016) and i can't really make sure that the date is the one i intended it to be
if any one can pleas point me to a solution it will be highly appreciated
if it makes any difference this is my code:
$Date = $args[0]
$Time = $args[1]
try
{
$tmpDate = $Date +" " +$Time
[DateTime]$UserDate = $tmpDate
}
catch
{
$tmp = $Date
$tmp -match "(?<d>.*)/(?<m>.*)/(?<y>.*)">$null
$_Date = $matches['m']+"/"+$matches['d']+"/"+$matches['y']
$tmpDate = $_Date +" " +$Time
try
{
[DateTime]$UserDate = $tmpDate
}
catch
{
write-host "Error: The Given Date was not recognized as a valid Date, Please Try again" -ForegroundColor Red
Exit 808040
}
}
The way most programs work is that they ask the user to input date in the acceptable format. Displaying something like YYYY/MM/DD near the input box or prompt will help the user understand what program will accept. Then whatever user entered you can validate each part separately and warn the user if any part of the date is wrong.
Related
I wrote the following deluge script, whene I am comparing the date of joining of the employee with the current date but i get a NULL response
Date of joining looks like 20-Nov-1979
searchMap1 = Map();
searchMap1.put("searchField","Dateofjoining");
searchMap1.put("searchOperator",">");
searchMap1.put("searchText",now);
Is there a solution to this?
I've managed to get my response from the application support team (in this case it is the zoho suite)
searchMap1 = Map();
searchMap1.put("searchField","Dateofjoining");
searchMap1.put("searchOperator","After");
searchMap1.put("searchText",zoho.currentdate.toString("dd-MMM-yyyy");
if you're comparing date strings it's easiest to print out both and then convert them to dates depending on their format:
info date_one; //output 20-01-2022
info date_two; //output 20/01/2022
if (date_one.toDate("dd-MM-yyyy") > date_two.toDate("dd/MM/yyyy")
{
//do this
}
I have a formula in Crystal Reports to convert for a particular date. The formula is working, but converts the field to a string, and I need the field to be a date as I am exporting as a CSV file and then loading data to a table.
Is there a way to use this same logic but have the field remain as a date
and not a string? Below is the current logic that I am using:
if totext((Date(Year({Command.RENEWAL_DT}), Month({Command.RENEWAL_DT}), Day({Command.RENEWAL_DT}))),'dd/MM/yyyy') = "01/01/1800" then
"01/31/2099"
else
totext((Date(Year({Command.RENEWAL_DT}), Month({Command.RENEWAL_DT}), Day({Command.RENEWAL_DT}))),'MM/dd/yyyy')
Maybe like this
if totext((Date(Year({Command.RENEWAL_DT}), Month({Command.RENEWAL_DT}), Day({Command.RENEWAL_DT}))),'dd/MM/yyyy') = "01/01/1800" then
"DATE '2099-31-12'"
else
"DATE '" & totext((Date(Year({Command.RENEWAL_DT}), Month({Command.RENEWAL_DT}), Day({Command.RENEWAL_DT}))),'yyyy-MM-dd') & "'"
Or use the TO_DATE() function.
I'm working on a project (online game) where users have the ability to use a coded 'airport' to travel to different countries & cities. Once they have travelled, the cityid within the database will update for example:
cityid = 1 (England - London)
cityid = 2 (USA - LA)
Therefore if a user travels from England to the USA it will store database side cityid = 2.
Now,
Whilst this is functional, I wish to incorporate timezone changes into it (if at all possible) I have tried:
if ($user->cityid == 3)
{
$timestamp ='1502448414'; //Timestamp which you need to convert
$dt = new \DateTime("#$timestamp");
$destinationTimezone = new \DateTimeZone('Mexico/General'); // To which timezone you need to convert
$dt->setTimeZone($destinationTimezone); // Set timezone
echo 'Mexico: '. $dt->format('H:i a'), "\n"; // Echo your changed datetime
}
As you can see, I have used Mexico as a demo to try and figure out my approach.
However, this only fetches the timestamped time rather than the realtime. Im aware, I could simply add the timestamp into a database table and then run a cron every second to update the table but this seems rather a long winded route.
Now within app.php the standard setting is UTC which runs as a realtime clock by returning: date('H:i:s').
My question is (after a lot of google searching) is there a way to manipulate this to make date() output the new time of (USA) when it has been travelled to?
Apologies that I cannot add anymore coding into this question, I have no real idea of how to approach it other than the one stated above.
Using Carbon
if ($user->cityid == 3) {
$dt = \Carbon\Carbon::now("Your current location timezone");
$dt->setTimeZone('Mexico/General');
echo 'Mexico: '. $dt->format('H:i a'), "\n";
}
I have to check whether coupon is active, or no by checking that today is between start date and expired date.
My start date and expired date format is 01/12/2017
Let assume, today is 21/06/2017
Coupon start date is 19/06/2017 and expired date is 23/06/2017
So it should result in Coupon is Active, wheter today is less than coupon start and expired date it should result in Coupon is Expired
So far I didn't find any tutorial which comparing today with start and expired date using Carbon.
Almost all tutorial like this
$now = Carbon::now();
$end_date = Carbon::parse($request->input('end_date'));
$lengthOfAd = $end_date->diffInDays($now);
So How I can check if today is between start date and expired date, and today is less than start date and expired date using Carbon in Laravel?
Thanks in advance.
It's a very simple task using Carbon, all you can do is call the between method of carbon :
$now = Carbon::now();
$start_date = Carbon::parse($request->input('start_date'));
$end_date = Carbon::parse($request->input('end_date'));
if($now->between($start_date,$end_date)){
echo 'Coupon is Active';
} else {
echo 'Coupon is Expired';
}
You can add a third parameter to the between method to use or equal like discribed in the doc
To determine if the current instance is between two other instances you can use the aptly named between() method. The third parameter indicates if an equal to comparison should be done. The default is true which determines if its between or equal to the boundaries.
The DB2 query below should be run with the date that the user entered. How can I pass the user input date into the DB2 query which one highlighted in Bold.
#/usr/bin/ksh
db2 connect to sunrise user edwprod using prodedw1
echo "enter the date (format should be 20140829)"
read $DATE # Read date here
result=`db2 "Select ORBDDT, count(*) as cnt_lines,
sum(ODAPRC * (cast(((coalesce(odsqty,1)/(case when odconv=0 then 1 else odconv end))) AS decimal (20,2)))) as sum_Sales,
sum(ODSQTY * ODAVCS) as sum_COGS
from SFBASLIB.SFORDD
JOIN SFBASLIB.SFORDR ON (ODORD# = ORORD# and ODLINK = ORLINK and ODSHP# = ORSHP#)
JOIN SFBASLIB.SFORDH ON (OHORD# = ORORD# and OHLINK = ORLINK and OHORD# = ODORD# and OHLINK = ODLINK)
where ORBDDT>='$DATE'
and ODSQTY <> 0
and ODSTAT not in ('SCR','CAN','BCN','KIL')
and ORPSTA not in ('SCR','CAN','BCN')
and ODITYP not in ('M','C')
and OHTYPE not in ('RE','QU','TRAN')
group by ORBDDT with UR"`
echo $result
Assuming that the second invocation of db2 was surrounded by back quotes (as now shown in the reformatted question), then the problem is in the read line. It should be:
read DATE
You don't prefix the name with $ in a read statement. That was taking the value assigned to $DATE and trying to use that as the variable name, which doesn't work.
You should probably use $(db2 "SELECT …") notation in preference to the back quotes. I assume you're going to do something more than just echo the result; otherwise, you shouldn't capture the output. And even if you do just echo it, you should use:
echo "$result"
to preserve the internal spacing of the result.
I assume that you are able to create the connection with the first db2 command and then run the query against the same connection with the second db2 command. And you'd better not have disclosed a production password.