DateRangePicker bootstrap plugin display one format send another - javascript

I have a dashboard that is requesting data from a servlet, the servlet is pulling data from a DB and needs the time formatted for a timestamp :YYYY-MM-DDThh:mm:ss+00:00. I have a parser on the servlet set up to parse this string to an sql timestamp.
I'm using the daterangpicker Bootstrap plugin. I would like to display the date format on the dashboard like so: DD/MM/YYYY hh:mm - DD/MM/YYYY hh:mm
I'm currently formatting the date like so: YYYY-MM-DDThh:mm:ss - YYYY-MM-DDThh:mm:ss
Then in my JavaScript I'm slicing it to the format I need to send:
var startDate = $('#range').val().slice(0, 19) + '+00:00';
var endDate = $('#range').val().slice(22, 41) + '+00:00';
I could obviously do a lot more slicing to achieve this, but maybe there's an easier solution?
I've tried two solutions I found on stack but both methods failed to pull the dateRange as a dateRange object: this and this

Related

DateTime format on server

I have multi select check-box to select report dates. User is choosing one or more report dates. I am using JavaScript to get selected dates:
var dateTime = new Date($(this).val()).toJSON();
I am passing selected dates to controller and creading query:
foreach (var reportDate in reportDates)
{
predicateReportDate = predicateReportDate.Or(p => p.LastUpdateDate == reportDate.ReportDate);
}
Everything works fine locally but I found issue with date format when I deploy page to server.
Locally DateTime format is like
'2017-04-21 00:00:00'
but on server I see totally different date:
4/20/2017 6:00:00 PM
It causes that filter does not work properly on server (no results are returned) because there is no report with that date.
Both:
LastUpdateDate
and
reportDate.ReportDate
are DateTimes (not strings)
Can you please help me with unifying date format?
What I did is:
I replaced line which reads date from filter:
var dateTime = new Date($(this).val()).toJSON();
with
var dateTime = $(this).val().split(' ')[0];
It seems that toJSON() is converting date (I am using UTC) to local server timezone.

Working with date formats in Java and MySQL

Ok this is really bugging me.
I am developing a web app and I need to work with dates. When a date is displayed in a view, or whenever a date is entered into a form I need the format to be dd/mm/yyyy.
What data type do I choose for my SQL database columns which contain dates. 'Date' doesn't seem to work, do I use varchar?
But If I use varchar how do I use java script to perform arithmetic with dates.
Do I do some conversions server-side?
Please advise the best practices.
Also Im using laravel if theres any useful stuff already built in.
Date is the correct type to use in SQL DB.
To access the value use ISO date format "YYYY-MM-DD HH:mm:ss" .
You can create it from Java Date Object by using toISOString() method.
For easier time format conversion I can also recommend to check out Moment.js.
Best practice to save the date in MySQL table as date field only. Which saves the date string in YYYY-MM-DD HH:mm:ss format.
You need to make sure the following things.
Before inserting date into MySQL change the format of date string to YYYY-MM-DD HH:mm:ss.
When you retrieve the date from database convert the date string from YYYY-MM-DD HH:mm:ss to your desired format.
You can use SimpleDateFormat class in Java to convert the dates
format. Use format() function to format the date in desired and
parse() function to get the Java date object from string.
Reference: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You should use varchar2 in mysql.
You can retrieve that varchar type date in javascript and create date object.
var d=dbdate;
var date = new Date(d);
And you can perform all javascript functions on date.
Hope it will help.
Javascript Date() object supports separate entries of date by:
var date=new Date();
var y=date.getFullYear();//4 digits
var m=date.getMonth()+1;//0-11 digits, plus 1 for true state
var d=date.getDate();//1-31
var dateSQL=y+'-'+m+'-'+d;//i.e 2016-07-25
you can use MySQL filed as datetime and also insert datetime format but when you will show then process it as you want as like
when you insert value in table then you can also format it as like
<?php $mysqltime = date ("Y-m-d H:i:s", $phptime); ?>
where $phptime is your input variable
$str = suppose $row['date'] (mysql filed value)
date("d/m/Y", strtotime($str));
where $str your retrieve date filed

Compare ISO date without time stamp in javascript

I am querying data using OData, url looks like http://myurl.com/api/Customer?$filter=ResDate eq DateTime'2014-03-15T12:01:55.123'.
I'm getting date/time from jquery.datepicker instead of the static date and using moment.js to convert from DD-MM-YYYY to YYYY-MM-DDTHH:mm:ss in order to pass it to web service.
function convertDateToISOdate(date){
var newDate = moment(date,'DD-MM-YYYY').format('YYYY-MM-DDTHH:mm:ss');
return newDate;
}
Date returns from the function, is 2014-03-15T00:00:00.
Problem : 2014-03-15T12:01:55.123 is not equal to 2014-03-15T00:00:00, so there's no record selected.
What I want is , just to compare the date , not include time stamp.
Note : I can not change the format date/time at server side(Web service) because it's not belongs to me.
Any idea is much appreciated.
Your first call to moment(date,'DD-M-YYYY') is stripping the time information from the incoming data. try using moment(date) (no format) instead because momentjs recognizes your incoming date format intrinsically, without having to be told which format to use, and will correctly parse the H:M:S data, too.
MomentJS date parse information

JavaScript moment format date string

I am using the Moment JavaScript library to take a date from my jQuery UI date picker. The date picker is currently using a format of mm/dd/YYYY in order to display a value to a user, so today would show as: 12/17/2013. I need to submit this date in an XHR request using the format YYYYmmdd so the example above would turn into 20131217. I tried using Moment like this:
var t_date = moment($("#datepicker").attr('value').replace('/', '-'), "MM-DD-YYYY");
The result for t_date however, comes out to be a large double, almost like a unix timestamp. At that point, I'm not entirely sure how to have Moment convert it to the string result I need above so I can embed it into a RESTful URL for my XHR request.
you can use the format function:
var t_date = moment($("#datepicker").attr('value').replace('/', '-'),'MM-DD-YYYY')
.format('YYYYMMDD');

angular.ui DatePicker how to set the Date format?

I am using the angular.ui DatePicker
http://angular-ui.github.io/bootstrap/#/datepicker
In the doc it says:
"Everything is formatted using the date filter and thus is also localized."
I am having a hard time figuring out how to use the date filter to control the date format set in the model.
Using the DatePicker I am getting a date format set in the model like this:
"2013-09-07T03:18:43.000Z"
However, I am trying to serialize with Grails, and I want my date format to be like this:
"2013-09-07T03:18:43Z"
How do I configure the DatePicker to output this format of date? Or since it is a JavaScript date, do I have to do this at the server side? Ultimately, I would like to send:
"2013-09-07T03:18:43Z"
in a JSON put to the server.
TIA
You can follow this trademark way of parsing the default ISO date format to your own format stripping out the milliseconds, in client side.
Or, in the server side (Grails) you can parse the string to your own format as below:
Date.parse("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "2013-09-07T03:18:43.000Z")
.format("yyyy-MM-dd'T'HH:mm:ss'Z'")
//prints "2013-09-07T03:18:43Z"
If you are persisting a Date instead of a String, just use Date.parse(..) (without .format).

Categories