Javascript convert string to epoch [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
I have the following string format : 2022-10-11 13:54:41.96 and want to convert it to epoch time using JavaScript , i tried the following but get a 'NAN' ,any idea how to convert such string to epoch using javascript?
var t ='2022-10-11 13:54:41.96'
var x = new Date(t).getTime()

You can simply replace " " with the separator "T" and use new Date() with the value.
var t ='2022-10-11 13:54:41.96'
var x = new Date(t.replace(" ", "T")).getTime()
console.log(x);
If you can control the time in "t" then simple add the separator "T":
var t ='2022-10-11T13:54:41.96'
var x = new Date(t).getTime()
console.log(x);

Try this should do the trick
var t ='2022-10-11 13:54:41.96'
var x = Number(new Date(t))

Related

Number prediction from 1 to 10 from javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
This is my code but when i run this code in console always giving me Congratulations.
Help me for solve this problem.
var val=Math.floor(Math.random() * 10) + 1;
console.log(val);
var Predict = Number(prompt("Prediction ?"));
for(var i=1 ; i <= 3; i++){
if(Predict<val){console.log("Up")};
if(Predict=val){console.log("Congratulations") };
if(Predict>val){console.log("Down")}
}
Equal operator assigns the right hand to the left hand and so the result is always true! To compare two values use double equals like this:
if (Predict==val){console.log("Congratulations") };

Javascript: Regex for "DD/MM/YY HH:SS" not succeeding [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have the following datetime string: 09/07/2020 02:00
I need to check the format of the string before apply Date.parse() to ensure that it's in the correct format. I've developed a regular expression for the string however it doesn't appear to work in Javascript. I've tested it and it works in regex tester but not in js. Example (regex101) here
REGEX
String
09/07/2020 02:00
Expression
^([1-9]|([012][0-9])|(3[01]))(\/)([0]{0,1}[1-9]|1[012])(\/)\d\d\d\d (20|21|22|23|[0-1]?\d):[0-5]?\d$
function clean_inputdatetime(datetime) {
var cleaned_datetime = null
var datetime_regex_cond = RegExp('/^([1-9]|([012][0-9])|(3[01]))(\/)([0]{0,1}[1-9]|1[012])(\/)\d\d\d\d (20|21|22|23|[0-1]?\d):[0-5]?\d$/', 'g')
var regex_success = datetime_regex_cond.test(datetime)
if (regex_success) {
var cleaned_datetime = Date.parse(datetime)
}
return [regex_success, cleaned_datetime]
}
console.log(
clean_inputdatetime("09/07/2020 02:00")
)
Note regex_success returns false for above.
When using RegExp you need to remove the enclosing slashes - otherwise they will be included in the regex itself. Also you need to correctly escape the string:
const datetime_regex_cond = new RegExp('^([1-9]|([012][0-9])|(3[01]))(\\/)([0]{0,1}[1-9]|1[012])(\\/)\\d\\d\\d\\d (20|21|22|23|[0-1]?\\d):[0-5]?\\d$','g')
Or simply do:
const datetime_regex_cond = /^([1-9]|([012][0-9])|(3[01]))(\/)([0]{0,1}[1-9]|1[012])(\/)\d\d\d\d (20|21|22|23|[0-1]?\d):[0-5]?\d$/g;

Return the first "n" count of characters of a string ( Javascript ) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I've used this :
String.Prototype.left = function left(count){
return this.substr(0,count);
}
And apply it this way :
var string = "Hello Stack!";
console.log(string.left(5));
And the console tells me :
TypeError: example.left is not a function
console.log(example.left(5));
How can I fix it? And where is the problem?
String.prototype.left = function(index){
return this.substring(0,index);
}
var str = "Hello World";
console.log(str.left(5));

JavaScript match() function does not give output [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
In JavaScript code I use regular expression match() function to find pattern and show the result, but it does not give a proper answer, means finding [i] character in giving sentences
<button onclick = "searchPattern()">pattern</button><br>
<p id = "demo"></p>
<script>
function searchPattern(){
var str = "Visti W3Schools!";
var paat = /[i]/g;
var result = str.match(patt);
document.getElementById("demo").innerHTML = result;
}
</script>
Read your JavaScript console
Uncaught ReferenceError: patt is not defined
You changed your variable name from paat to patt half way through your code.

Javascript var inisde array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I want to use the value of a variable inside a array, like this.
var advalue = Math.floor(Math.random * 6);
var ads = ["Hello", "World", "Randomword", "123", "1233", "0000"];
API.sendChat("Word: " + ads[advalue]);
When i run this, i get undefined
There is a typo, missing brackets after random
var advalue = Math.floor(Math.random() * 6);

Categories