I'm trying to get a date value from a form. What I tried to do here is to get the value then compare that value to a regular expression. Then I tried to create a date object where I set the date value to the result of the match. My console just returns NaN, even when I enter the correct format. Any ideas on how I can modify this so that I can set the date value correctly?
var adate = document.getElementById("dueDate").value;
var datestring = new RegExp("[0-9]{4}\\.\[0-9]{2}\\.\[0-9]{2}");
var stringmatch = adate.match(datestring);
var date = (new Date()).setDate(stringmatch);
console.log(date);
if (checkInputText(date, "Please enter a due date")) {
return;
}
when I change up the regex a little and do this, I get a value for stringmatch and date, with yours I don't.
var adate = "11/09/2288";
var datestring = /^(\d{2}[.\/-])(\d{2})([.\/-]\d{4})$/;
var stringmatch = adate.match(datestring);
var d = new Date(stringmatch[0]);
var date = d.setDate(stringmatch[2]);
Date.setDate(day) <-- usage, I am not sure what you are trying to do here. In my hardcoded date, would you want "09" to be setDate? I am trying to wrap my brain around what you are trying to accomplish.
Related
I have a Datetime String Saved to a cookie as below
DateTime date = DateTime.Now;
LoginDate = date.ToString("u", DateTimeFormatInfo.InvariantInfo).Replace("Z", "");
int sessionTimeout = 1;
DateTime dateExpress = date.AddMinutes(sessionTimeout);
ExpressDate = dateExpress.ToString("u", DateTimeFormatInfo.InvariantInfo).Replace("Z", "");
HttpCookie cookie = HttpContext.Current.Request.Cookies["express"];
if (cookie == null)
{
cookie = new HttpCookie("express");
}
cookie.HttpOnly = false;
cookie.Path = "/";
cookie.Values["express"] = ExpressDate;
Response.Cookies.Add(cookie);
This work as expected as I can see in the Application tab that cookie has saved correctly
Please refer the image below
But when I access it from the client side it only returns the date bit losing the time portion.
var current = getCookie("express");
var date = current.split(" ")[0];
alert(date);
What am i doing wrong here?
Since your express contains express=2017-01-16 09:07:49 and when we split it apart by space you will get two string in which first will be date and another next would be time. Thus you have to do something like this to get the date and time separated.
var current = getCookie("express");
var date = current.split(' ')[0];
var intime = current.split(' ')[1];
alert(date);
alert(intime);
Whereas the current.split(" ")[0]; will give you expression like express=2017-01-16 and thus I believe you should break this apart using = again to get the date only.
or else if you are looking for date and time in one variable you can do something like this
var current = getCookie("express");
var smDateTime = current.split('=')[1];
alert(smDateTime);
your cookie is in the format "yyyy-mm-dd hh:mm:ss"
split that string on a space ... .split(" ") ... you get an array
["yyyy-mm-dd", "hh:mm:ss"]
element [0] is "yyyy-mm-dd"
solution
var date = getCookie("express");
alert(date);
How do I convert the value 24/05/2016 (typeof(24/05/2016) is number) for a Date using JavaScript?
While performing the following error occurs:
transactionDateAsString.split is not a function"
var transactionDateAsString = 24/05/2015;
var parts = transactionDateAsString.split("/");
var dudu = parts + '';
var date = new Date(dudu[2], dudu[1] - 1, dudu[0]);
console.log(date);
Not sure why you are adding an empty string at some point. This is how it should be:
var transactionDateAsString = '24/05/2015';
var parts = transactionDateAsString.split("/");
var date = new Date(parts[2],parts[1]-1,parts[0]);
alert(date);
Also note the quotes around the date as string.
I was trying to convert a Number to Date in Javascript. Below is the code which I have tried
var newDate = new Date(1012256900000);
console.log("Test: ",newDate.toString('MMM-yyyy'));
This is working fine.
But when I get it from $rootScope object, am getting invalid date :(
var newDate = new Date($rootScope.lastLoginTime);
console.log("Test: ",newDate.toString('MMM-yyyy'));
This is printing Invalid Date. FYI,
$rootScope.lastLoginTime = 1463256900000;
Gonna take a guess here, but look at what I did in the browser console:
new Date(1463256900000)
> [date] Sat May 14 2016 21:15:00 GMT+0100
new Date("1463256900000")
> [date] Invalid date
Completely wild guess, but perhaps you should ensure you are passing a number, not a string, to new Date() - the constructor behaves very differently in either case.
Consider instead trying this:
var newDate = new Date();
newDate.setTime($rootScope.lastLoginTime);
setTime takes a numeric argument only, and will convert your string to a number for you if you pass it one.
Guess your $rootScope.lastLoginTime = '1463256900000'; than $rootScope.lastLoginTime = 1463256900000;.
new Date(time) does works when time is number , not when time is string.
var t1 = 1463256900000;
var t2 = '1463256900000';
var d1 = new Date(t1);
var d2 = new Date(t2);
console.info(d1);
console.info(d2);
I have 2 TextBoxes that expect a date in the format mm/dd/yyyy
Ex:
03/20/2013
Before I even bother to make the ajax call, I want to try and turn these into JS Dates. How could I check if:
Both Dates are in the mm/dd/yyyy format and if they are, then From must be less than to.
Thanks
I'd recommend using moment for this one. Moment is a js library especially for dates, and evaluating dates. Your two text boxes start off as strings, so you need to init 2 moment()'s with each. Then verify they're both moment objects. If so, it's then a simple matter to make sure the one is after the other.
Here's the link to moment: http://momentjs.com/
Here's code I may use:
var tb1 = $("#tb1").text(); // get string from "date box 1"
var tb2 = $("#tb2").text(); // get string from "date box 2"
//get timestamp val's so they can be used in moment initialization
var date1 = tb1.split("/");
var date2 = tb2.split("/");
//create moments
var mom1 = moment([date1[2], date1[1], date1[0]);
var mom2 = moment([date2[2], date2[1], date2[0]);
function validate(mom1, mom2){
//validate both dates are actual dates
if (mom1.isValid() && mom2.isValid()){
//compare timestamps to ensure 2nd timestamp is larger
if(mom1.unix() < mom2.unix()){
return true;
} else {
return false;
}
} else {
//strings aren't dates, return error
$.error("not valid dates");
}
}
I'm having a form which is having two input fields with date picker(with the help of jquery ui).One asking a check in date & the other asking check out date.I want to calculate the Number of Days Between those two dates.I am a beginner to javascript.So can any one help me?
Use unix timestamp. It's universal tool when you working with time:
var a = new Date(2012,11,10);
var b = new Date(2012,11,12);
var dt = (b.getTime() - a.getTime())/(24*3600*1000) //2
You must first get the value of such input fields, eg:
var v1 = document.getElementById('input1Id').value;
var v2 = document.getElementById('input2Id').value;
then you have to instantiate two new Javascript Date objects:
var parts1 = v1.split('/');
var parts2 = v2.split('/');
// this will split your string into date parts, eg. 11/30/2012 would result as an array ['11','30','2012'];
var date1 = new Date(parts1[2],parts1[0],parts1[1]);
var date2 = new Date(parts2[2],parts2[0],parts2[1]);
// remember to check the index of array items considering your date format
finally, you just have to subtract one date from the other to get the difference in days:
var difference = Math.Abs(date1 - date2);
difference = parseInt(difference / 86400000);
//86400000 are the milliseconds in one day. Parsing to int will round the day - eg.5,4 days results as 5 day
Edit: Sure, you can reference your html element by his id attribute
<input type="text" id="myTextbox">
---
var txb = document.getElementById('myTextbox');
// do anything else to your txb here, if u like
txb.value = difference;
that's it!