Where does Date() get its values from - javascript

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.

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.

Moment.js, Moment-timezone: input and display in eastern timezone?

I've got a javascript app that the user insists that regardless of location of the app user, they want date/times entered by the user in Eastern time. That they will be sent to the database and stored, and later displayed again in the app and displayed in Eastern timezone.
So I was looking to use moment.js and moment-timezone, but can't figure out how to make it work.
When I try const objMoment = moment(), objMoment appears to contain a time offset from the current time.
I was wanting to get a moment object that is whatever time, and then objMoment.tz("America/New_York")
but I ended up with a time that is 5 hours off from what it should be.
const objMoment = moment();
const objEastern = objMoment.clone().tz("America/New_York");
debugger;
formData.SwoDate = objMoment.toDate();
debugger;
A few things:
You don't need to create a local moment and clone it. You can instead construct a moment directly in a specific time zone with:
moment.tz('America/New_York')
Since you call toDate(), the result is always going to be a Date object that represents "now". It doesn't matter if that comes from a Moment that's been set to a different time zone or not. It's the same moment in time.
moment().valueOf() // 1600198416842
moment.utc().valueOf() // 1600198416842
moment.tz('America/New_York').valueOf() // 1600198416842
new Date().valueOf() // 1600198416842
Date.now() // 1600198416842
If you want to see the time in a different time zone, you would need to use a function like format instead.
moment.utc().format() // "2020-09-15T19:33:36Z"
moment.tz('America/New_York').format() // "2020-09-15T15:33:36-04:00"
Moment is a legacy library. You should probably choose a different library, or perhaps no library at all. Please read: Moment Project Status

Dates - JavaScript and C#

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.

Save the actual date in a variable (only date, no time)

I want to save the actual date in a variable. only the date, no time
var a = #Date(#Now());
datasource.replaceItemValue("variable", a)`
And
var a = #Date(#Now());
var b = new Date(a.getYear(), a.getMonth(), a.getDay());
datasource.replaceItemValue("variable", b)
Are returning 28.10.14 00:00
var dt:NotesDateTime = #Date(#Now());
datasource.replaceItemValue("variable", dt.getDateOnly());
Is throwing me an error
Isn't there a simple way to get only the actual date without the time?
Use setAnyTime() metohd of NotesDateTime class to remove time component.
If you want to save only the date use a textfield and convert the text to date, if you need it in your code
#Now uses a java.util.Date, which includes time portions. .getDateOnly() is probably throwing an error because that returns a String.
The underlying session.createDateTime() method accepts either text, a java.util.Date or a java.util.Calendar. I think all of them need to include a time element.
If you're only storing the value for reference, I'd agree with brso05 to not worry.
If you're ever likely to use #Adjust (or an equivalent), then not worrying about the time is a recipe for disaster, because every time you try to adjust, you need to remember to take into account Daylight Savings Time.
One option is to set the time to 12:00 midday. That means DST will not be a problem.
java.sql.Date is specifically designed to only include the Date portion, without a time element. Jesse Gallagher talks about java.sql.Date in the context of his frostillic.us framework https://frostillic.us/f.nsf/posts/32A63DD640D868D885257D18006659A9 and he was the one I found out about java.sql.Date from. I'm not sure how he stores those values though.
I'm not sure if the OpenNTF Domino API will allow you to just pass a java.sql.Date to a field and so store just the date portion.

updating a Date object on the client side after getting the correct Date from the server side

I have a UI that needs to have the UTC time updated all the time to be shown to the users.
my original code was this:
(function () {
$http.get('api/getdate').success(function (data) {
current = new Date(data.res + new Date().getTimezoneOffset()*60*1000);
updateDateTimer = setInterval(function(){
$http.get('api/getdate').success(function (data) {
current = new Date(data.res + new Date().getTimezoneOffset()*60*1000);
});
}, 30000);
});
})();
data.res has the correct timestamp i need. this way works fine because i can trust my server with the data that is sent back to the UI. then the only data that i need from the user is its offset from UTC and thus i am sure the date that will be displayed will be correct.
my problem is that a call to the server will be made every 30 seconds, and if i want my clock to change even more often then this can get really nasty.
i thought about making the call to the server only one time and then add 1 second with a timer to the Date object created but this is very not accurate and after a few minutes you can see that the clock is not synchronized any more with the real time.
is there any possibly to not make more calls to the server after the first time?
the problem is that i can't make sure the client won't change its local clock. it seems i need to some how deal with the Date object that is being created with the first .get call, but how?
thanks
Could you please clarify the question a bit? If the only thing you're trying to do is find/show the current UTC time, new Date().getTime() returns the millis since January 1, 1970 UTC so that time is already in UTC (2 calls of getTime() occurring simultaneously in New York and Los Angeles will show the same number). Then you have some UTC functions on the Date object which give you the UTC time, like getUTCHours(), getUTCMinutes and getUTCSeconds().

Categories

Resources