var dateNum = Number('/Date(1306348200000)/'.replace(/[^0-9]/g,''));
var formattedDate = new Date(parseInt(dateNum.substr(6)));
alert(formattedDate);
What's wrong with this code? Why does it not execute and give me the desired result...
Try this.
var formattedDate = new Date(parseInt(dateNum.toString().substr(6)));
Felix's comment is the answer :)
I've no idea why you're doing it in a complicated way - / / is for regular expressions, not for dates. Then you also have this notation inside a string. I'm not aware of any /Date(...)/ format. What you're doing on the first line is parsing the number out of it, but why not do it yourself?
This works fine:
var formattedDate = new Date(1306348200000);
alert(formattedDate);
To format it, you would need certain functions to combine the date components as described here:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date#Methods_2
Interesting function you have here. I see at least one problem dateNum is not a string.
Might be a good idea to submit what you expect to get from your code.
Related
As a beginner I am trying to understand if there is a way to convert this into a one-liner? Have tried to search of course before asking. I need an output in Selenium that outputs HHMM without : . So, I need to know if there's a way to put this into oneline?
const now = new Date();
console.log(now.getHours() + "" + now.getMinutes());
Thanks a lot for helping out!
This could be an option
console.log(new Date().toTimeString().split(':').slice(0,2).join(' '))
I'm kinda new to Javascript.
My problem is the following : I have to implement a form that is generated by a js file, by taking the url and paste it inside my HTML. I have a date input, and I need to verify if the date is right, wether it is dd/mm/aaaa or mm/dd/aaaa.
If possible, without using regExp. I already made this function out of StackOverflow RegExp :
function validateDateRegExp(field){
var date=field.value;
var regExp1 = /^(((0[1-9]|[12]\d|3[01])[\s\.\-\/](0[13578]|1[02])[\s\.\-\/]((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)[\s\.\-\/](0[13456789]|1[012])[\s\.\-\/]((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])[\s\.\-\/]02[\s\.\-\/]((19|[2-9]\d)\d{2}))|(29[\s\.\-\/]02[\s\.\-\/]((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/;
var regExp2 = /^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[1-9])|(1[0-2]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$/;
if(!(regExp1.test(date)||regExp2.test(date))){
alert(date);
document.getElementById('fld_CS_BuyingDate').value="";
}
}
I'm looking for a solution since 2 days, but the answers are not clear, or the problem is not the same.
Does anyone have any idea on how I could work that out ?
Thanks for the future answers
I need some help with validating a date time string in Javascript, based on the browser's language.
I can get the datetime format easily enough, for instance if the language is set to pt-BR the format would be
dd/MM/yyyy HH:mm:ss
I tried using something like this:
var dateFormat = "dd/MM/yyyy HH:mm:ss";
var x = Date.parseExact($("#theDate").val(), dateFormat);
However x is always Null. I am thinking because Date.parseExact is not able to do times. I need to be able to do this for all browser languages and I would prefer to not use another library. Using Regex is out also since I would need to write so many different expressions.
Does anyone have any suggestions to help me ge on the right track? I am also not against using a webmethod.
I have tried using the following webmethod, which works with en-US but nothing else:
Public Function ValidateDates(ByVal strDate_In As String) As String
Dim theFormat As String = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern() + " " + CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern()
Try
Dim d As DateTime = DateTime.ParseExact(strDate_In, theFormat, CultureInfo.CurrentCulture)
Return "true"
Catch ex As Exception
Return "false"
End Try
End Function
You can use Regex to do this:
var dateFormat = "dd/MM/yyyy HH:mm:ss";
var x = $("#theDate").val().match(/^(\d{2})\/(\d{2})\/(\d{4}) (\d{2}):(\d{2}):(\d{2})$/);
console.log(x);
Demo: https://jsfiddle.net/kzzn6ac5/
update
The following regex may help you and improve it according to your need:
^((\d{2}|\d{4})[\/|\.|-](\d{2})[\/|\.|-](\d{4}|\d{2}) (\d{2}):(\d{2}):(\d{2}))$
It matches the following format with /.- and yyyy/mm/dd hh:mm:ss or dd/mm/yyyy hh:mm:ss
Updated demo: https://jsfiddle.net/kzzn6ac5/1 or https://regex101.com/r/aT1oL6/1
Further Regex expressions relevant to date matching can be found here.
JavaScript date objects are deceptively easy, I worked with them in a project and they had a sneaky learning-curve that takes a lot of time to master (as opposed to the rest of JavaScript, which is relative child's play). I recommend letting VB, or really anything else handle it.
But if you want a way to do it in javascript, without Regex (as stated in your question), you could perform string operations on it like this:
try {
var user_input = $("#theDate").val();
var split = user_input.split(" "); // 0: date, 1: time
var split_time = split[1].split(":"); // 0: hours, 1: minutes, 2: seconds
d.setHours(split_time[0]);
d.setMinutes(split_time[1]);
} catch {
// not in a valid format
}
This solution assumes the input is in the correct format, and if an error occurs, it's not. It's not the best way of doing things, but JS Date objects are seriously horrible.
I came across Datejs recently and found it very useful. However I could not figure out if there is a way to parse a string and extract only date part from it using the same.
For example, if there is a string >> "I will start exercise from next Monday."
Then it should parse the string, extract 'next monday' from it and convert it into date and give me the result.
How can it be implemented?
Thanks :)
You can write some RegEx for that. That would be the easiest way. 'next' will be a keyword in that case. A simple function can lookup the current weekday and return the date of the next monday. Should not that complicated.
Edit:
You can do something like this:
var pattern = /^([\w\W.]*)(next){1}([\sa-zA-Z]*)/;
while (result = pattern.exec(yourTextVariable) != null){
// read the data as you need from the result array
}
The Pattern above expecting a white space then the keyword next and will red the next word if it only has alpha-letters. (please note that the RegEx is untested and may need some refactoring to fit your needs. You may take a look at this page to do this: javascriptkit.com)
I have asp (classic) script and within javascript code. From database I get date in format yyyy-mm-dd (2010-10-14) and save in variable. Then, I pass this variable to javascript method:
Response.Write("<a href='Javascript: PassDate("&OurDate&","&Val1&","&Val2&");'>Pass</a>")
This method code is:
function PassDate(OurDate, Val1, Val2)
{
window.open("newsite.asp?date="+OurDate+"&val1="+Val1+"&val2="+Val2");
}
When I try get date on new site (newsite.asp) by Request.QueryString("date"), I get calculate value 1996 (2010-10-14 = 1986), instead date '2010-10-14'.
I try various ways to solve this problem, but it still calculate value.
For example, I try replace "-" for ".", but I get error about missing ")".
Use commas instead of dashes. That way, each part will be seen as a separate argument to the JavaScript function.
Have you considered saving the date using the javascript date object and then passing that to your method?
My javascript is a little rusty, but I believe it would be something like the following:
var dateParts = split(OurDate, "-");
var myDate = new Date(dateParts[0], dateParts[1], dateParts[2]);