Ruby on rails - convertion of bootstrap datepicker value to ruby time value - javascript

In my form view I use bootstrap daytime picker
<input type="text" value="" placeholder="select date you want to send" class="form-control" name="send_date" id="datetimepicker" />
<script>
$.datetimepicker.setLocale('<%= session[:locale] %>');
$('#datetimepicker').datetimepicker({
minDate: 0,
dayOfWeekStart : 1,
lang:'<%= session[:locale] %>',
startDate: '-0m',
format:'d-m-Y H:i:s',
});
</script>
In my SMS controller:
def send_sms
message = params[:message]
number = params[:number]
send_date = params[:send_date]
format = "%Y-%m-%d %H:%M:%S UTC"
send_date = DateTime.strptime(send_date, format)
SendSmsTimeJob.set(wait_until: send_date).perform_later(number, message)
flash[:success] = "mesaj gitti"
redirect_to current_user
end
When I submit the form it says:
ArgumentError in SmsController#send_sms
invalid date
I think that problem is I cannot convert input value to ruby time value? How can I overcome with this?

Change your format to this.
format = "%d-%m-%Y %H:%M:%S"

In your datepicker you format your date as format:'d-m-Y H:i:s' which means days-month-years
In your ruby code you parse it differently:
format = "%Y-%m-%d %H:%M:%S UTC"
First you have the date order reversed: year-month-days
Second you add another expected value 'UTC' which is not included in the string you are trying to parse.
Change your format to format = "%d-%m-%Y %H:%M:%S" as suggested by Rubysmith

You can try Date.parse params[:send_date] instead.

Related

How to select a date in a datepicker 21 days from curreny date always with selenium java

This is my code:
SimpleDateFormat df = new SimpleDateFormat("dd/MM/YYYY");
Date dt = new Date();
Calendar cl = Calendar.getInstance();
cl.setTime(dt);;
cl.add(Calendar.DAY_OF_YEAR, 21);
dt=cl.getTime();
String check_date = df.format(dt);
System.out.println("the depature date is " +check_date);
js.executeScript("document.getElementById('txtFromDatesFgt').value='check_date'");
When I run this code then date is stored in string and output will be displayed on console, but how do I send this string into .value='');?
If you inspect the HTML you can find an hidden input element for the date
<input data-val="true" data-val-required="*" id="txtFromDateFgt" name="StartDate" type="hidden" value="19/03/2020">
you can update the value of this field instead.
Try this:
driver.findElement(By.id("txtFromDatesFgt"])).setAttribute("value", check_date);

Getting the correct format of the datepicker

I have a column inside of my datebase that stores a date that user pick when they offer a job. It's currently formed in this way: "Fri Feb 15 2019".
I want to be able to compare it to the current date which is formed as: "2019-01-27"
I'm guessing that the best way to do this would be to transform the datepicker format to the date one, and then compare them. This is the code that I have for the date picker currently when someone registeres.
<div class="form-group">
<label for="location" class="control-label">DATE</label>
<input type="text" id="datepicker" class="form-control" placeholder="Pick a
date" name="datepicker">
</div>
<script>
var picker = new Pikaday({ field: document.getElementById('datepicker') });
</script>
Open the example in full page :
Use new Date and then you can extract the day the month and the year
and to compare with another date, you can just new Date("2006-05-1").getMonth() === new Date("2006-05-2").getMonth() && ...
var picker = new Pikaday({
field: document.getElementById('datepicker')
});
const dateInput = document.getElementById('datepicker');
function handleChange() {
const date = new Date(dateInput.value),
formattedDate = `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`;
console.log(formattedDate)
}
dateInput.addEventListener("change", handleChange);
<script src="https://cdn.jsdelivr.net/npm/pikaday/pikaday.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/pikaday/css/pikaday.css">
<input type="text" id="datepicker">
I would suggest using the javascript plugin called Momentjs. It allows for easy formatting of dates, without the base javascript hassle.
Add this to your HTML, near the bottom of your page, but before your script tags:
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
Then to format your datepicker value, inside your script, use:
var myDate = moment(picker.value).format('YYYY-MM-DD');
Here is the Momentjs documentation:
Momentjs Docs

Not right date is displayed

I use this daterangepicker: https://github.com/dangrossman/daterangepicker
I receive a date from database: so the format is: YYYY-MM-DD
Web client don't necessary use this format.
I tried that
<input type="text" id="samplingsBuildDatePicker" name="receptionDate" class="form-control">
$('#samplingsBuildDatePicker').daterangepicker({
singleDatePicker: true
});
var dateStart = moment("2018-01-01", 'YYYY-MM-DD', true).format();
$('#samplingsBuildDatePicker').data("daterangepicker").setStartDate(dateStart);
Current day is displayed instead of the first day of the year...
Edit
I added a text area
$("#remark").val(dateStart);
It's the correctly value who is displayed: 01/01/2018... date picker don't working
The problem is here var dateStart = moment("2018-01-01", 'YYYY-MM-DD', true).format();
You are receiving date from DB in YYYY-MM-DD format and you want to convert to DD/MM/YYYY.
Check the below working example for how to do this:
var dateStart = moment("2018-01-01").format("DD/MM/YYYY");
console.log(dateStart);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script>
So, make the following changes:
var dateStart = moment("2018-01-01", 'YYYY-MM-DD', true).format();
Change to:
var dateStart = moment("2018-01-01").format("DD/MM/YYYY");
You are Done!

How make validation of Date with datetime work with jquery(date picker) for php?

Script(jquery) use to make input[date] accessible to multiple browser(datepicker)
<script>
$(function(){
if (!Modernizr.inputtypes.date) { //if browser doesn't support input type="date", initialize date picker widget:
// If not native HTML5 support, fallback to jQuery datePicker <script src="js/realtime_validityScript.js">
$('input[type=date]').datepicker({
// Consistent format with the HTML5 picker
**dateFormat: 'dd-mm-yy',**
maxDate: 0
},
// Localization
$.datepicker.regional['it']
);
}
});
</script>
2- PHP code for validating date format by user in case jquery is disabled (not working) - also not sure how to use DateTime::getLastErrors()
// specify your date's original format, in this example d/m/Y (e.g. 25/07/2014)
**$format = "d-m-Y";**
$date = DateTime::createFromFormat($format, $_POST['DateField']);
$date_errors = DateTime::getLastErrors();
if(? $date_errors[also dunno what to put here?] ) {
// createFromFormat returns false if the format is invalid;
echo 'Your date format is incorrect';
} else {
//change it to any format you want with format() (e.g. 2013-08-31)
$date->format('Y-m-d');
}
Original PHP code for getting my date: (my db access currently this format "Y-m-d")
if (empty($_POST["DateField"]) ) {
$errors['DateField'] = "Age is required";
} else {
$date = date("Y-m-d", strtotime( $_POST['DateField']));
}
I also want the input date to be able to convert formats like those ('d-m-Y', 'Y/m/d', 'Y.m.d', 'd.m-Y', 'd/m/Y') to 'Y-m-d' maybe with an array of some sort?
Note: if i remove the 2- part my code work fine
Thanks you for your support...still new to PHP would really help if someone could expl to me what wrong and what the correct code
Date type will not allow your required formats. so instead of using type="date" use type="text" and add a class date to the input field to initialise datepicker like,
....
$('input.date').datepicker({
// Consistent format with the HTML5 picker
dateFormat: 'dd-mm-yy',
maxDate: 0
},
....
And change HTML like,
<input type="text" class="date" ..../>

Date Picker Highlight day if not Available

I have this online reservation, and i have this problem, Is there a way i can highlight the unavailable days on the date picker?assuming i have inserted dates on the database. Here's my code for the date picker.
<input type="text" name="pick-up-date" id="pick-up-date" class="form-control datepicker" placeholder="mm-dd-yyyy">
Here's a screenshot:
Use beforeShowDay() function from jQuery check this
e.g., something like this,
var datesToDisable = ["2015-01-07","2015-17-07","2015-21-07"]; //you have to fetch the dates and put em in an array and wh7atever the format is.
$('#pick-up-date').datepicker({
beforeShowDay: function(date){
var string = jQuery.datepicker.formatDate('dd-mm-yy', date);
return [ datesToDisable.indexOf(string) == -1 ]
}
});
Should do the trick!

Categories

Resources