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.
Related
First of all thanks in advance for helping, the community is great.
I have a problem parsing my date and time. Here is my code:
var date = mail.bodyText.match(/\=\= date \=\=\s*(.*[^\s*])/);
if (date) {
var string1 = date[1].match(/^\d{4}\-\d{2}-\d{2}/);
var string2 = date[2].match(\s(\d{2}\:\d{2}\:\d{2}));
var string3 = date[3].match(\s(\+\d{4}));
var parts1 = string1.split("-");
var parts2 = string2.split(":");
if (parts1 && parts2)
{
var dt = new Date(parseInt(parts1[0], 10), parseInt(parts1[1], 10) - 1, parseInt(parts1[2], 10), parseInt(parts2[3], 10), parseInt(parts2[4], 10), parseInt(parts2[5], 10));
}
date_final = dt;
}
date_final is defined elsewhere, and is in Date Time Picker format, and here is the input I am trying to parse:
blabla
== date ==
2016-02-13 16:22:10 +0200
blabla
Every time I execute the code, I get a parsing problem. The variable date_final cannot handle the parsed date. What do you think is missing from this code?
Update:
Here is what I'v etried out. Impossible for me to locate what's wrong:
var date = mail.bodyText.match(/\=\= date \=\=\s*(.*[^\s*])/);
if (date) {
var initial = date[1];
var formated = initial.substring(0, 19);
var final = formated.replace(/-/g, '/');
var last = new Date(final);
Field = last;
logging += "{date=" + Field + "}";
}
The code is actually parsing an email and sending the result over SSL. What surprises me the most is that the logs keep posting the following output of the date i naddition to the "parsing issue": date=Sat Feb 27 2016 16:22:10 GMT+0200 (CEST).
Do you think the problem comes from the code or could be related to how the appliance this code implemented on can handle it?
Thanks
Jane
Sorry for answering in comment.
Here's one solution to your question:
var dateStr = '2016-02-13 16:22:10 +0200';
// get yyyy-MM-dd HH:mm:ss
var formatedStr = dateStr.substring(0, 19);
// get yyyy/MM/dd HH:mm:ss in case of working on most of the browsers
var finalStr = formatedStr.replace(/-/g, '/');
// Date object can easily parse the datetime string we formated above
var date = new Date(finalStr);
Date object can parse complex strings.
Mail providers usually follow an RFC on how timestamps should be written, thus allowing other programming languages to heavily support it.
Just pass your string into date object and it will convert it for you.
let mailStr = `blabla
== date ==
2016-02-13 16:22:10 +0200
blabla`;
let regex = mailStr.match(/\=\= date \=\=\s*(.*[^\s*])/);
let dt = new Date(regex[1]);
console.log(dt);
The output is described in ISO-8601
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);
I need to get a date in this format:
2016-07-06T10:57Z
Using this code I have been able to get a date in a format somewhat like I need:
var isoDate = new Date().toISOString();
2016-07-06T08:46:08.127Z
But is there a way I can remove the seconds and fraction of seconds from the date so it appears exactly like the date: "2016-07-06T10:57Z" ?
You will always want to remove the last 8 characters ('Z' included) thus you can use a function like slice
isoDate = isoDate.slice(0, -8); //Remove seconds + fractions + Z
isoDate += "Z"; //Add back the Z
You can use this way because the format returned by toISOString() will always be
YYYY-MM-DDTHH:mm:ss.sssZ
Please try
var isoDate = new Date().toISOString();
var pos = isoDate.lastIndexOf(':');
var datePart1 = isoDate.substring(0,pos);
var datePart2 = isoDate.substr(-1, 1);
var dateStr = datePart1+datePart2;
console.log(dateStr);
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.
I have inserted random date into a textbox let it be 12/2/2009 now I want only 2 in this how to do it with javascript how do I need array or any other method.
Please give me an appropriate example
Use :
var mydate = $("textboxId").val();
var dateArr = mydate.split("/");
var date = dateArr[0];
var month = dateArr[1];
var year = dateArr[2];
You'll get month from 'dd/m/yyyy' formatted date by reversing the date
new Date("12/2/2009".split("/").reverse().join("/")).getMonth()
Check here