jQuery datepicker UI getdate() - How do I just output date only and NOT Wed Oct 06 2010 17:00:00 GMT-0700 (Pacific Daylight Time) - jquery-ui

Currently this code is working but not as expected:
$("#start_date").datepicker({
dateFormat: 'yy-mm-dd',
onSelect: function(_date, _datepicker)
{
var myDate = new Date(_date);
myDate.setDate(myDate.getDate()+8);
$('#estimated_hatching_date').text(myDate);
alert( myDate);
}
});
The first issue is, how the date is presented. Currently that code above alerts Fri Oct 01 2010 17:00:00 GMT-0700 (Pacific Daylight Time). I would like the output to be just the date, for example, 09/06/2010.
The second issue is, $('#estimated_hatching_date').text(myDate); does not change the text. When that code is fired nothing is changed. However if I do: $('#estimated_hatching_date').text('myDate'); myDate is placed into the #estimated_hatching_date div.
So, how do I just output the date and replace the "text" inside of #estimated_hatching_date div with just the date +7 days from when a date is selected?
Thank You,
Rich

jQuery UI Datepicker has a built in formatDate function. Please #dottedquad, don't use your self-made version, date math and formatting is harder than you imagine. The corner cases will make you tear your hair out.
onSelect: function(_date, _datepicker) {
var myDate = new Date(_date);
var myText = $.datepicker.formatDate('dd-mm-yy',myDate);
alert(myText);
}

I read over the ui datepicker manual again and figured it out.
$("#start_date").datepicker({
dateFormat: 'yy-mm-dd',
onSelect: function(_date, _datepicker)
{
var myDate = new Date(_date);
myDate.setDate(myDate.getDate()+8);
var fullYear = myDate.getFullYear();
var month = ((myDate.getMonth()+1) < 10) ? ('0' + (myDate.getMonth()+1)) : myDate.getMonth()+1;
var day = (myDate.getDate() < 10) ? ('0' + myDate.getDate()) : myDate.getDate();
var myNewDate = fullYear + '-' + day + '-' + month;
$('#estimated_hatching_date').text(myNewDate);
alert(myNewDate);
}
});
That code works as expected. Anyone else have any more ideas that would get that job done differently?
-Thank You,
Rich

I had the same problem and used "altField" option with a hidden input. Hidden input's value is always formatted according to "dateFormat"

Related

Swift comparing date is not working and getting wrong time

Hi I have multiple dates which I am comparing with current date. But it is not working. I have done coding but nothing is working for me. I am using comparison same like this link.
Here is what I am doing
let todayDate = Date()
let date1FromServer = //its Date from server which is 2020-02-01 09:00:00 +0000
let date2FromServer = //its Date from server which is 2020-02-02 09:00:00 +0000
let date3FromServer = //its Date from server which is 2020-02-03 09:00:00 +0000
now here I am comparing All three date in simple if else
if(date1FromServer < todayDate){
//Do something for date 1
}else if (date2FromServer < todayDate){
//Do something for date 3
}else if (date3FromServer < todayDate){
// Do something for date 3
}
Case: Now I have date1 and Date 2 is working but date3 is not working. I am saying this because the date3 is exactly on 3 feb 2020
with 9:00 am which is smaller then today date which is 3 feb 2020 with
11:00 am , but it looks like that it is dealing like date 3 is equal
or greater then today date.
When I print date 3 it prints (2020-02-03 06:32:22 +0000) and here the time is wrong, as right now on my device it si 11:32 am but it showing 06: 32 . I think as I am just printing it in log so there could be problem of time zone, but on above code it is also not working.
Please let me know what is the problem here.
Note: I want to compare the Date and time as well.....
If your server date has GMT as time zone then you can adjust your local date to GMT by using offsets
let offsetDate = Date(timeIntervalSince1970:
todayDate.timeIntervalSince1970 + Double(TimeZone.current.secondsFromGMT(for: todayDate)))
and then use offsetDate instead when doing the comparison

How to format date while using alasql

I am using this following way while doing an export to excel
alasql.fn.Date = Date;
alasql('SELECT new Date(mydateString) AS CUSTOM_DATE INTO XLS("' + filename + '.xls",{headers:true}) FROM ?', [items]);
Those above lines are printing in this below date format
Sat Jun 06 2015 00:00:00 GMT-0500 (Central Daylight Time)
but I want to format mydateString to this below format
MM-DD-YYYY hh:mm:ss
How can I do that?
I did this below instead and create my own format for date
alasql.fn.datetime = function(dateStr) {
var date = new Date(dateStr);
return date.toLocaleString();
};
alasql('SELECT datetime(mydateString) AS CUSTOM_DATE INTO XLS("' + filename + '.xls",{headers:true}) FROM ?', [items]);

ui Datepicker - Format the Date after selection

I am running an Ajax call to get places available for a certain date.
I can get that date fairly comfortably with the following code :
$('.booking-date').datepicker(
{
beforeShowDay: enableAllTheseDays,
numberOfMonths: 2,
dateFormat: 'DD, d MM, yy',
showButtonPanel: true,
onSelect: function(dateText, inst) { }
}
I know the dateFormat is there, but I ideally want that to be in a nice neat format for the end user, Which produces something like : Sunday, 28 July, 2013
If I can switch the DateFormat for the dateText variable to a MySQL friendly date that would be even better.
Hope someone can help
Cheers
Solved by creating the following :
onSelect: function()
{
var dateText = $.datepicker.formatDate("yy-mm-dd", $(this).datepicker("getDate"));
$('.date_hidden').html(dateText);
}

Date with Time only get's NULL

I am using the Grails plugin, http://grails.org/plugin/jquery-date-time-picker
The date and datetime plugin works perfectly well. However, just time has an issue.
I tried to use time only for the datetimepicker. My Config.grovy is as below.
jqueryDateTimePicker {
format {
java {
datetime = "dd/MM/yyyy HH:mm"
date = "dd/MM/yyyy"
}
picker {
date = "'dd/mm/yy'"
time = "'hh:mm tt'"
}
}
}
My GSP code is something like.
<jqueryPicker:time name="openFrom" id="openFrom" value="${addressInstance?.openFrom}" pickerOptions="[dateFormat: '', timeOnly: true, hourGrid: '4', minuteGrid: '15', timeFormat: 'hh:mm tt']"/>
My Controller is
def addressInstance = new Address(params)
Here in the controller, i can see the params having the time as "7:00 am" but it never gets set in the addressInstance, i believe cause the date is missing.
The default Date binding takes the general date format as yyyy-MM-dd hh:mm:ss.S.
In order to to bind openForm date to addressInstance, you can explicitly set it as:
//where params.openForm is String like "7:00 AM"
addressInstance.openForm = Date.parse("h:mm a", params.openFrom)
Date.parse() parses the string as current format to return a Date object. In the aboved case(where you are only concerned about the time), you would end up with the epoch (Jan 1, 1970) with time as 7 AM.
def date = Date.parse("h:mm a", "7:00 AM")
//prints Thu Jan 01 07:00:00 EST 1970
//To get the time string from the date stored in db
date.format("h:mm a") //Prints 7:00 AM
You can also see if you can register a Custom property Editor as shown here to auto bind the date with customized format. You would not like to follow this if you do not want to apply the format to all date strings. In that case, I think the former approach will be useful and easy.

NSDateFormatter equivalent in Titanium / Convert date in specified format in Titanium

I'm working with the date and time in my Titanium based iOS application.
My issue is, I'm getting the time in the below format:
Wed Jan 16 2013 17:32:40 GMT+0530 (IST)
I want to change it like:
Jan 16, 2013, 05:32 pm
In iOS there is NSDateFormatter for doing this.
What I did :
Currently I'm doing it manually using split and switch cases, but it fails if the input time format changes, then I need to re-write the code for the changed format.
I searched alot in Titanium Docs, but didn't get any solution.
I asked same question on their forum, didn't get any reply till now.
Is there is anyway to do this in Titanium?
Please help, Thanks in advance.
Look for moment.js JavaScript module for date formatting
I didn't find any alternatives for the NSDateFormatter in Titanium.
I tested the Moment.js, but it didn't worked for my case.
I implemented a JavaScript function for achieving my output.
/**
* Function for formatting the date and time
* #param {Object} dateTime
*/
function formatDateTime(dateTime)
{
var arr = dateTime.split(' ');
var convertedDate = arr[1]+' '+arr[2]+', '+arr[3];
var convertedTime = arr[4];
var returnValue = convertedDate+', '+convertedTime;
return returnValue;
}
Edit on 27 Feb 2014
After facing the JavaScript date formatting issue on different occasions, I finally developed my own JavaScript module, this module will format the JavaScript Date object to Specified formats. You can find it here : MMP_DateTime
I write one function which will convert date as per my requirement as given below
var getRequiredDate = function(date) {
var monthList = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var day = date.getDate();
var month = date.getMonth();
var year = date.getFullYear(); // Use standard date methods
var newdate = monthList[month] + ' ' + day + ", " + year;
Ti.API.info("Date :" + newdate);
};
Hope this helps you.

Resources