This question already has answers here:
How to clone a Date object?
(8 answers)
Why javascript date.setDate() change the value of other date variables [duplicate]
(3 answers)
Closed 1 year ago.
I am trying to subtract 5 day from giving date however when I am doing it, its change by original date also.I couldn't understand why this is happening. please see below my code.
var HARVESTDATE= new Date("2021-02-16T05:00:00.000Z");
console.log('HARVESTDATEdate', HARVESTDATE);//2021-02-16T05:00:00.000Z
let rangeDate = HARVESTDATE;
rangeDate.setDate(rangeDate.getDate() - 5);
console.log('rangeDate', rangeDate);//2021-02-11T05:00:00.000Z
console.log('HARVESTDATE', HARVESTDATE);//2021-02-11T05:00:00.000Z
In above code I have given date as 2021-02-16T05:00:00.000Z and I want 5 days back date as 2021-02-11T05:00:00.000Z which is assign to variable rangeDate however when its doing it it change my HARVESTDATE also which I don't want to change. could anybody help me what issue with it?
thanks
let rangeDate = HARVESTDATE;
rangeDate and HARVESTDATE are the same object.
If you modify rangeDate, HARVESTDATE also changes.
What you have to do instead is this:
let rangeDate = new Date(HARVESTDATE);
Related
This question already has answers here:
How to parse JSON to receive a Date object in JavaScript?
(17 answers)
Closed 5 years ago.
In my code I get JSON response as /Date(1238626800000)/.
I want to convert this object to Unix Timestamp. So I would like to know that whether is there any default javascript or jquery method which can convert it to Unix Timestamp ?
So My Input Date is: /Date(1238626800000)/ and
Output I want is: 1238626800000
I can do it with RegEx but this is last option if no default method available
No need to use regex here. Just slice out the timestamp:
if (value.startsWith("/Date(") && value.endsWith(")/"))
return new Date(Number(value.slice(6, -2)));
like this:
var input = '/Date(1238626800000)/';
var re = /Date\(([0-9]*)\)/;
var ret = re.exec(a);
if(ret) {
input = ret[1];
}
This question already has answers here:
Javascript date regex DD/MM/YYYY
(13 answers)
Closed 7 years ago.
I want to find Dates in a document.
And return this Dates in an array.
Lets suppose I have this text:
On the 03/09/2015 I am swiming in a pool, that was build on the 27-03-1994
Now my code should return ['03/09/2015','27-03-1994'] or simply two Date objects in an array.
My idea was to solve this problem with regex, but the method search() only returns one result and with test() I only can test a string!
How would you try to solve it? Espacially when you dont know the exact format of the Date? Thanks
You can use match() with regex /\d{2}([\/.-])\d{2}\1\d{4}/g
var str = 'On the 03/09/2015 I am swiming in a pool, that was build on the 27-03-1994';
var res = str.match(/\d{2}([\/.-])\d{2}\1\d{4}/g);
document.getElementById('out').value = res;
<input id="out">
Or you can do something like this with help of capturing group
var str = 'On the 03/09/2015 I am swiming in a pool, that was build on the 27-03-1994';
var res = str.match(/\d{2}(\D)\d{2}\1\d{4}/g);
document.getElementById('out').value = res;
<input id="out">
This question already has answers here:
Javascript Thousand Separator / string format [duplicate]
(15 answers)
Closed 8 years ago.
I've been trying to figure out how to insert commas into the result of this function for an hour now.
var i = 2574672248;
function increment2() {
i++;
document.getElementById('xxx').innerHTML = i;
}
setInterval('increment2()', 50);
The result is incremented by 1 every half a second. I'm trying to make it so, that if the result is 2394672248 for example, it's shown as "2,574,672,248" instead of the raw "2574672248". I've tried incorporating different comma inserting functions into it, but it's not working.
var i = 2574672248;
function increment2() {
i++;
document.getElementById('xxx').innerHTML = i.toLocaleString('en');
}
setInterval('increment2()', 50);
From here: add commas to a number in jQuery
This question already has answers here:
How to get the first element of an array?
(35 answers)
Closed 8 years ago.
I am trying to get the first word out of the variable var solution = [cow, pig]
I have tried everything from strings to arrays and I can't get it. Please help.
As per the comments
solution[0]
Will return the first item in the array.
solution[1]
would be the second, or undefined if the array was:
var solution = [cow]
Is solution an array, or is it in that form? (var solution = [cow, pig]) You also need to add quotes around those values, unless those values are defined variables.
You need to change the variable to look like this:
var solution = ['cow', 'pig']
If so, just get the value at subscript 0.
var result = solution[0];
console.log(result);
If you mean an string like
solution = "cow pig".
Do
solution = solution.split(' ')[0];
console.log(solution); //Will return cow
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Simplest way to parse a Date in Javascript
I understand how do get the data and break it down into it's segments, i.e.
alert( ( new Date ).getDate() );
and
alert( ( new Date ).getFullYear() );
alert( ( new Date ).getFullMonth() );
etc etc.
But how do I do the same but use a date from a html textbox? instead of reading new Date?
The date in the HTML box would be formated as follows
31/10/2012
You could try:
var datearray = input.value.split("/");
var date = new Date(datearray[2],datearray[1] - 1,datearray[0])
If your textbox has proper string format for a date object you can use:
var aDate = new Date($("textbox").val());
However, if you dont write it in the text box exactly as you would in a string passing to the object, you'll get null for your variable.
FYI, I made a plugin that "extends" the Date object pretty nicely and has preformatted date/times that include things like a basic SQL datetime format.
Just go to this jsFiddle, Copy the code between Begin Plugin and End Plugin into a js file and link it in your header after your jQuery.
The use is as simple as above example:
var aDate = new DateTime($("textbox").val());
And to get a specific format from that you do:
var sqlDate = aDate.formats.compound.mySQL;