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];
}
Related
This question already has answers here:
Separate string into multiple variables with JavaScript
(3 answers)
Split a string straight into variables
(4 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 1 year ago.
I want day,month and year values from the value in an object.
{myDate : '12/31/2020'}
I want to do something like this:
d,m,y = mydate('/')
so that d= 12, m=31 , y = 2020 and they are separated using the '/' character
Is there a way to do something like this in JS?
Split the string and destructure the resulting array:
const myDate = {myDate : '12/31/2020'}
const [d,m,y] = myDate.myDate.split('/')
console.log(d,m,y)
const [d,m,y] = '12/31/2020'.split("/")
console.log(d,m,y)
Use
mydate = "12/30/2021";
var arr = mydate.split("/");
//arr[0]=12
//arr[1] is day
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);
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 5 years ago.
aa=&bb=0108135719&cc=20180108135935&dd=ee&ff=201801081358544265&gg=1&hh=1000&ii=&
i have a string like this and i trying to get specific value from it, it is in text format. what can i do if i want to get value of 'aa' it null and value of 'bb' it is 0108135719 tats it. I'm tried different regex but unable to get the desired output.
You can do something like
var t="aa=&bb=0108135719&cc=20180108135935&dd=ee&ff=201801081358544265&gg=1&hh=1000&ii=&".split("&")
console.log(t)
var ansObj = {}
t.forEach((element) => {
const elementArray = element.split("=")
const key = elementArray[0]
const value = elementArray[1]
ansObj[key] = value
})
console.log(ansObj)
This question already has answers here:
endsWith in JavaScript
(30 answers)
Closed 9 years ago.
Is there a javascript function to get the last occurrence of a substring in a String
Like:
var url = "/home/doc/some-user-project/project";
I would like a function that returns true if the String contains project at his end.
I know str.indexOf() or str.lastIndexOf() but is there another function that do the job or should I do it?
Thanks for the answer
Something like
var check = "project",
url = "/home/doc/some-user-project/project";
if (url.substr(-check.length) == check){
// it ends with it..
}
Try this
<script>
var url = "/home/doc/some-user-project/project";
url.match(/project$/);
</script>
The response is a array with project, if the responde is 'null' because it is not found
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;