Dates - JavaScript and C# - javascript

I hate dates, I can never get them to behave.
I have a javascript variable that looks like this:
var currentDate = new Date();
I pass this to a C# Web API controller as a parameter.
My local time was 12:43 but when I put a breakpoint in my action it shows 11:43. The problem is, that if I do this at 00:43 then my controller would take the date as yesterday. I need it to pick out the right day. If I select the currentDate as 02/09/2015 12:43 then I need my controller to use the same date.
I know this has something to do with local times etc, but how can I get them all to use the same one?
Any help would be greatly appreciated.

Related

Is the IF function removing the date object in javascript?

I've spent an hour looking for answers and trying different things so I appreciate any help here.
The following code works great for finding someone's part B effective date. However, when someone's birthday is really on the 1st of a month the 'if' function get's used, and I'm no longer able to format and write the date. It's almost like 'partB_eff' is no longer a date object. (I'm a newbie, so I might just be making this part up.)
I'm getting the error "TypeError: partB_eff.toLocaleDateString is not a function at AutoFill_6_Step_Checklist(Code:24:27)"
How can I resolve this?
let birthday = new Date(e.values[2]);
//this is a date entered from a google form
let bdayCopy = new Date(birthday);
//I created this since I'll be using .setMonth(), and I don't want to change the original date of the birhtday
let bday65 = new Date(bdayCopy.setMonth(bdayCopy.getMonth()+780));
//finds the 65th birthday
let partB_eff = new Date(bdayCopy.setDate(01));
//find's the Medicare part B effective date (the 1st of the month someone turns 65)
if(birthday.getDate()==1){
partB_eff = partB_eff.getMonth-1;
//if the person's birthday is really on the 1st of the month, the part b effective date is the 1st of the month prior. partB_eff must be converted
}
partB_eff = partB_eff.toLocaleDateString('en-us',{year:"numeric",month: "short",day:"numeric"});
//format partB_eff so that it looks nice on paper
partB_eff = partB_eff.getMonth-1;
Doesn't do what you think it does. What it does is get the vound function getDate from your date object, and attempt to subtract one from it. In any other language trying to do subtraction on a function would be a type error, but Javascript is Javascript and allows numeric operations on almost any type. A function minus a number in JS is NaN. NaN doesn't have a method called toLocaleString, hence the error.
What's interesting is that you did the same operation correctly above with bdayCopy.setMonth(bdayCopy.getMonth()+780)
Just do the same thing here
bdayCopy = new Date(bdayCopy.setMonth(bdayCopy.getMonth()-1));
Also some important concepts. if in Javascript is not a function. if is a keyword that starts a conditional statement. You can't do any of the things you can do with a function with if. You can't call it or assign it to a variable or pass ot as a function argument. Clearly understanding what a function is is something you need to do to be able to work in JS, or frankly any other language.
Finally if you are doing date math in JS I strongly recommend you use a date library like DateFns or Moment. Javascript native date APIs are possibly the worst designed date API of any language ever.

Where does Date() get its values from

In my code, I have the following line:
var today = new Date();
My research tells me this should set today as the current date. But where does this current date come from?
In one instance, I set an object up with this current date. Then my app wants this object to timeout after 30 days, so I changed the date on the tablet and re-entered the app (it closed and reopened), and yet today still gets the same value from Date(). Where does Date() get its value from?
If I wanted to get a tablet specific time, how would I do that?
When you call new Date, the value is get from the current computer/table browser time and store it in variable.
If you want to get value for specific date you shoud pass the date like that:
var today = new Date('2016-11-11') // will get the 11 November date
Maybe this answer is useful to you too. The Date is taken from your current system.
I figured out my problem. Apparently, my tablet's date/time was being set back to the current value every time I tried to go into my app. I don't know why it did this, but when I disconnected from the internet I was able to see the correct 2 months in advance that I wanted.

Efficient way to run code based on time

I want to run come specific code after specific timing but with a efficient technique, Not with SetTimeout/SetInterval.
Like request to Database to check data on a after specific time dialy.
I want to query database at after 5:00 pm. So i write this code.
function checkVotingQuestionTime{
var currentDate = new Date();
if(currentDate .getHours =>5){
//go to database
}
}
But I have to write this code in setInterval(checkVotingQuestionTime, 60000);
Is there any better way to do this job? I guess current code is performance killer. register event etc
I would recommend using a 3rd party solution like
http://bunkat.github.io/later/
or
http://bunkat.github.io/schedule/
The first will overwrite setInterval() for you, so that you can use later schedules instead of providing a timeoffset in milliseconds:
https://bunkat.github.io/later/execute.html#set-interval

how to find hh:mm:dd difference between two strings in Javascript

In my app I have two date strings:
Say that they are:
date1 = "2014-03-14 18:25:15";
date2 = "2014-03-14 16:26:15";
I get these date strings based on two events that the customer selects. Now I need to show the difference between these two strings in HH:MM:DD format.
What I am currently doing is, posting to PHP using AJAX and then doing the calcuation in the server:
$rDate = new DateTime($date1);
$tDate = new DateTime($date2);
$interval = date_diff($rDate,$tDate);
echo $interval->format('%h:%i:%s');
Then in the AJAX response handler I print it to a div
My problem is that server trip is just too much an overkill for this. How can I achieve the same thing from browser itself? (Javascript/Jquery/MomentJS)...
Any help is greatly appreciated!
I'd suggest looking into Moment.js, which is a very well featured date handling library for Javascript.
Here's the relevant manual link for Moment.js for what you're wanting to do: http://momentjs.com/docs/#/displaying/difference/
Hope that helps.

BIRT Using Date parameter in script

I am using BIRT and am trying to modify the text on a chart based on an end date report parameter, which I've assigned a default value and of course, Date datatype. SQL queries also use this parameter. To test passing of value, I write the below script.
function beforeDrawLegendItem( lerh, bounds, icsc )
{
var endDate = new Date(params["rptEndDate"].value);
}
However, when I try to preview the chart, it is blank, indicating that there is something wrong with my script. When I comment out the var endDate line, it renders as normal. I've tried other ways to get the value like:
var endDate = new Date();
Which works, but when I try to assign the parameter value:
endDate = params["rptEndDate"].value;
It fails again. Removing the .value part also fails. Also fails when I try to use reportContext to get the parameter value. According to other sources my script should be correct. Is there anything I am missing?
This is due to the specifity of the chart engine: "params" object is not made available in chart scripts, use "getParameterValue" instead. It will work like this:
function beforeDrawLegendItem( lerh, bounds, icsc )
{
var endDate = new Date(getParameterValue("rptEndDate"));
}
The Previewer tends to offer less then optimally formated or fully functional design. Open the report in the format/tool that will be used in the final delivery for true testing durring design. Personally I have Apache on my test server and do all of my preview testing as a web page.

Categories

Resources