DateTime format on server - javascript

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.

Related

Get one week before date compared to todays date in Node js in Mysql Format

I am looking to get the past date precisely one week from current date in sql format in node js .I tried this -How to get yesterday date in node.js backend? but it does'nt seems to work for me
Try Library called node-datetime
var datetime = require('node-datetime');
var dt = datetime.create();
// 7 day in the past
dt.offsetInDays(-7);
var formatted = dt.format('Y-m-d H:M:S');
console.log(formatted)

JSON date in javascript not works

I have a MVC web site, in some views I show a datetime from JSON value, frist I show month in one input/text control and the year in other control. The website is used by many cities and works fine, but in one city when I convert JSON date value to Javascript date value, date value has one month less, for example if JSON date is 2016-04-01 12:00:00 Javascript date value is 2016-03-01, I think is a configuration of the PC is this city but what configuration?
This is the code:
var Ffab = new Date(parseInt(item.Ffab_ettc.replace(/\/Date\((.*?)\)\//gi, "$1")));
$("#txtFfabTCM").val(Ffab.getMonth() + 1);
$("#txtFfabTCY").val(Ffab.getFullYear());
item.Ffab_ettc is the JSON Datetime value.

DateRangePicker bootstrap plugin display one format send another

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

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

Dates in .net vs in javascript

I have a ASP.NET WEB.API 4 and a Controller returning the following json:
{ date: "2013-03-14T00:00:00" }
I parse it on the client (JavaScript):
date = new Date(json.date); // json.date being "2013-03-14T00:00:00"
Later I do a POST and in the body the date has changed format to
{ date: "2013-03-13T23:00:00.000Z" }
My guess was that some time zone stuff has been added by JavaScript or the browser?
Because of DB storing dates as int (yyyymmdd) I do the following convertion:
public static int ToInt(DateTime date)
{
return date.Year * 10000 + date.Month * 100 + date.Day;
}
The resulting int is then one day off :(
However if I do
date.ToLocalTime()
before calling the ToInt method, it looks fine.
Have I understood this correctly and is ToLocalTime() a sufficient solution or do I need to spend a day reading up on UTC dates in .NET and JavaScript?
Thanks!
In my opinion you will get better result if your controller will return date in ZULU format. So before sending use: ToUniversalTime (http://msdn.microsoft.com/en-us/library/system.datetime.touniversaltime.aspx) Than the result also will be in ZULU format, so you don't have to convert it to LocalTime
date = new Date(json.date); // json.date being "2013-03-14T00:00:00"
And what timezone is that json.date in? If there's no information about this, the JS Date constructor will assume it is the client's local timezone (and that is quite random). If you want it to be UTC, just add "Z" after your ISO-datestring:
date = new Date(json.date+"Z");
Also always use the [gs]etUTC(FullYear|Month|Date|…) methods then.

Categories