I need to find EST current offset value using javascript - javascript

I need to find current EST offset value using javascript without any server side code
Because I am hardcoding Offset value in my javascript file,i need to pass dynamically because of Day Light Saving.
Kindly Help me on that
Thanks in advance..,

Using moment-timezone:
var currentUSEasternTime = moment.tz('America/New_York');
var minutesOffset = currentUSEasternTime.utcOffset(); // -240
var hoursOffset = minutesOffset / 60; // -4
var offsetString1 = currentUSEasternTime.format('Z'); // "-04:00"
var offsetString2 = currentUSEasternTime.format('ZZ'); // "-0400"
Not sure what output you were looking for, so gave you a few options.

Related

How to use JavaScript to replace an attribute inside a span tag?

I'm looking to update the data-value attribute's value in the code below with a dynamic integer value I will return in a small JS code I wrote. My code will generate a different integer based on a few factors that vary day to day, but in any case, it returns one single integer value that I want to insert into the data-value attribute.
I have a current solution that works but is "ugly" I think. I basically used DOM manipulation to hide the value the code below produces in the UI, and implemented my own counter from 0 to the dynamic integer value. Is there a more direct and "proper" way of solving the problem, something like innerHTML but maybe for attributes? Thanks!
<div class="content-box-percentage content-box-counter" style="color:#ffffff;font-size:60px;line-height:normal;">
<i class="counter-box-icon fontawesome-icon fa-mug-hot fas" style="font-size:70px;" aria-hidden="true"></i>
<span class="display-counter" data-value="21" data-direction="up" data-decimals="0">0</span>
</div>
Thanks
EDIT: adding the "ugly" code I had, as requested. Originally I had the class ID bsa-counter in the first code snippet. As I was looking to make it prettier (therefore the reason for my post in the first place), I removed the class ID and the JS code as I was planning on starting over.
"use strict";
// CounterBox multiplier - multiplies the number of days by an integer
const bsaMultiplier = 3;
// Customize counter boxes to support counting from a start date to the current date, in number of days
// Run custom counter box function, passing in bsaMultiplier
bsaCounterBoxToToday(bsaMultiplier);
function bsaCounterBoxToToday(bsaMultiplier) {
const bsaStartDateC = "2019/07/14"; // enter in the format of "YYYY/MM/DD", including quotes
const bsaDomElementId = "bsa-counter"; // DOM ID element to select
const bsaDomElementClass = "display-counter"; // DOM Class element to select
const bsaCounterDuration = 500;
// Date range
const bsaStartDate = new Date(bsaStartDateC);
const bsaToday = new Date(
`${new Date().getFullYear()}/${
new Date().getMonth() + 1
}/${new Date().getDate()}`
);
// Days * multiplier
const bsaCountToValue =
Math.ceil(Math.abs(bsaToday - bsaStartDate) / (1000 * 60 * 60 * 24)) *
bsaMultiplier;
let bsaCurrentValue = 0;
let bsaIncrSpeed = setInterval(() => {
bsaCurrentValue < bsaCountToValue
? (document
.getElementById(bsaDomElementId)
.getElementsByClassName(bsaDomElementClass)[0].textContent =
++bsaCurrentValue)
: clearInterval(bsaIncrSpeed);
}, bsaCounterDuration / 100);
}
I think this might be what you're looking for:
var yourInt = 4;
document.getElementsByClassName("display-counter")[0].setAttribute("data-value", parseInt(yourInt));
There are multiple ways of getting the span element's DOM, but this is one way if your span element is first on your html doc. Consider using an id and the "getElementById()" function instead:
document.getElementById("the-id-name-you-added").setAttribute("data-value", parseInt(yourInt));
change your span to be:
<span id="the-id-name-you-added" class="display-counter" data-value="21" data-direction="up" data-decimals="0">0</span>

BIRT: Weird behaviour of parseInt

I am using a report parameter in BIRT.
It a string, which contains the month/year, like: 08/2018
To test the value, I am using the following code. It is located in a Dynamic Text:
var dateStringArray = params["monthYear"].value.split("/");
var date = new Date(parseInt(dateStringArray[1]), parseInt(dateStringArray[0]) - 1, 1);
var t = parseInt(dateStringArray[0]);
t;
If I fill the parameter with 08/2018, I get a NaN, see:
But if I fill the parameter with 07/2018, it is working correctly:
I have tested it with several numbers. It is just not working with 08 and 09. All other number til 10 are working...
This seems to be a weird scenario. Need to raise a bug on this.
But for your workaround, you can make use of the code below in Dynamic Text which is working fine:
var dateStringArray = params["monthYear"].value.split("/");
var monNum;
if (BirtStr.charLength(dateStringArray[0]) == 2)
{monNum = BirtStr.right(dateStringArray[0],1);}
else {monNum = dateStringArray[0];}
monNum;
//var date = new Date(parseInt(dateStringArray[1]), parseInt(dateStringArray[0]) - 1, 1);
var t = parseInt(monNum);
t;

Subtract 1 from variable jQuery

Having some trouble getting this right. I'm very new to jQuery, so trying to get better and learn.
Currently I am getting 2 different values from a html table using the following code
var sellPrice = $('.qt').find("tr:eq(2)").find("td:eq(4)").html();
var buyPrice = $('.break .main-col .qt').find("tr:eq(2)").find("td:eq(4)").html();
These both output a value such as $13,000,000
I am then wanting to subtract 1 from these values (making it $12,999,999) before pasting them to an input as such
$('input[name="sell"]').val(sellPrice);
$('input[name="buy"]').val(buyPrice);
However, I am having some trouble with how to subtract $1 from these.
I tried using sellPrice--; but without success.
I've also tried adding - 1; at the end of each variable, but did not succeed either.
I tried to test something like this, but did not work either.
var minusOne = -1;
var getCurrentSellPrice = $('.qt').find("tr:eq(2)").find("td:eq(4)").html();
var getCurrentBuyPrice = $('.break .main-col .qt').find("tr:eq(2)").find("td:eq(4)").html();
var sellPrice = (getCurrentSellPrice - minusOne);
var buyPrice = (getCurrentBuyPrice - minusOne);
$('input[name="sell"]').val(sellPrice);
$('input[name="buy"]').val(buyPrice);`
Trying my best to familiarize myself with jQuery :)
Any help is much appreciated!
Solved using this
var getCurrentSellPrice = $('.qt').find("tr:eq(2)").find("td:eq(4)").html();
var getCurrentBuyPrice = $('.break .main-col .qt').find("tr:eq(2)").find("td:eq(4)").html();
var sellPrice = Number(getCurrentSellPrice.replace(/[^0-9\.]+/g,"")) - 1;
var buyPrice = Number(getCurrentBuyPrice.replace(/[^0-9\.]+/g,"")) + 1;
$('input[name="sell"]').val(sellPrice);
$('input[name="buy"]').val(buyPrice);
Since your numbers contain currency symbol and are strings, you need to convert them to proper numbers before subtracting them. See the answer below.
How to convert a currency string to a double with jQuery or Javascript?

getFullYear doesn't return anything

Im in the land of JavaScript and I'm trying some functions for my university project, but I got stuck in this part:
function validateDate(){
alert("validateDate");
var date = document.getElementById("dateN");
var yearN = data.getFullYear();
alert(yearN.value);
var dateA = new Date();
var yearA = dateA.getFullYear`enter code here`();
alert(yearA.value);
if(((yearA - yearN)<18) || ((yearA - yearN)>120)){
alert("age between 18 and 120 only.");
data.style.backgroundColor = "red";
}
}
When I try to print the values, nothing happens, Which terrible wrong thing Im doing here guys?
The dateN is comes from another part (which is working =D ).
Sorry if its a very "noob" question,
Thanks in advance!
There is two mistake here :
var date = document.getElementById("dateN");
var yearN = data.getFullYear();
date is refering a DOM element an it is not a date !
data is undefined !
maybe you want make a Date with the value from "dateN" ?
so according to the HTMLElement you have to get his value and create a new Date !
// saying is a input text and a valid date format !
var date = new Date( document.getElementById("dateN").value );
var yearA = dateA.getFullYear`enter code here`();
// ^
// this is not valid o----------------|
You are calling data.getFullYear() -
var yearN = data.getFullYear();
Don't you mean date.getFullYear??

UiApp createServerHandler parameter arguments

Here is the output from a button callback, as you can see it returns a reference to the picker control which is included in the grid control that they were both added to:
[13-09-06 21:59:07:575 ICT] {clientY=83, clientX=100, eventType=click, ctrl=false, meta=false, source=Today, button=1, alt=false, picker=Sat Sep 07 00:00:00 GMT+07:00 2013, screenY=399, screenX=649, y=16, shift=false, x=28}
Here is the code used to hook it all up:
var app = UiApp.createApplication().setWidth(600).setHeight(400);
var submitHandler = app.createServerHandler('eventFormData_Save_');
var dateBtnsHandler = app.createServerHandler('eventDateBtns_');
// create panels
var vPanel = app.createVerticalPanel().setStyleAttributes(css.body);
var sPanel = app.createScrollPanel().setId('sPanel');
// add vPanel to sPanel
sPanel.add(vPanel);
var dToday = getWholeDay(new Date());
var oDateGrid = app.createGrid(3,3);
var oBtnPrev = app.createButton('< Prev').setId('Prev');
var oBtnNext = app.createButton('Next >').setId('Next');
var oBtnToday = app.createButton('Today').setId('Today').setWidth('100');
var oTxtOldDate = app.createTextBox().setId('pickerOldDate').setWidth('50');
var oPickerLabel = app.createLabel('Selected Date:',false).setId('pickerLabel');
var oPicker = app.createDateBox().setId("picker").setName("picker").setWidth('100')
.setFormat(UiApp.DateTimeFormat.DATE_SHORT)
.setValue(dToday).setFocus(true);
oDateGrid.setWidget(0,1,oPickerLabel);
oDateGrid.setWidget(1,0,oBtnPrev).setWidget(1,1,oPicker).setWidget(1,2,oBtnNext);
oDateGrid.setWidget(2,0,oTxtOldDate).setWidget(2,1,oBtnToday);
dateBtnsHandler.addCallbackElement(oDateGrid);
//dateBtnsHandler.addCallbackElement(oTxtOldDate);//tried adding the txtbox explicitly to no effect
oBtnPrev.addClickHandler(outgoingClientHandler).addClickHandler(dateBtnsHandler);
oBtnNext.addClickHandler(outgoingClientHandler).addClickHandler(dateBtnsHandler);
oBtnToday.addClickHandler(outgoingClientHandler).addClickHandler(dateBtnsHandler);
oPicker.addValueChangeHandler(outgoingClientHandler).addValueChangeHandler(dateBtnsHandler);
oTxtOldDate.addValueChangeHandler(dateBtnsHandler);
app.add(oDateGrid);
it's not all the code as it rambles a bit and shouldn't be relevant, while some of what's there relates to later elements - just let me know if it's not clear!
Problem arises when trying to reference the pickerOldDate within the same callback function as for the buttons
{source=picker, eventType=valuechange, picker=Sat Sep 07 00:00:00 GMT+07:00 2013}
So I'm not sure what to be surprised by, the picker being in the button callback parameter, or the pickerOldDate not being in the picker callback parameter. Any advice?
BTW: the point (if it's not clear) is to retain an 'OldValue' for the picker in case users pick a date in the future (not allowed), I need to return the picker value to the one before the callback was invoked. If I've missed a trick in engineering this outcome, I'm happy to go another route - thanks!
Turns out you need to assign the name of any object you hope will appear in the callback, I had only set the Id and it cost me a few hours - Documentation may not be Google's strong point :)
var oTxtOldDate = app.createTextBox().setId('pickerOldDate').setName('pickerOldDate').setWidth('50');
Hope this helps someone else

Categories