How to pass variable in jquery set date - c#

I am trying to execute some JS code using C#:
executor.ExecuteScript("window.document.getElementById('pmtDate').setAttribute('value','08/16/2013');");
Instead of 08/16/2013 I would like to pass variable for Date.
Can anyone please let me know the syntax for this?

var temp_date='08/16/2013';
executor.ExecuteScript("window.document.getElementById('pmtDate').setAttribute('value',tempdate);");

My understanding, you want the date in mm/dd/yyyy format.
To get the current date, use the below code:
var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();
var today = (month<10 ? '0' : '') + month + '/' + (day<10 ? '0' : '') + day + '/' + d.getFullYear();
Now use this with your code.
executor.ExecuteScript("window.document.getElementById('pmtDate').setAttribute('value',"+today+");");

If I get you right:
executor.ExecuteScript("var date = '08/16/2013'; window.document.getElementById('pmtDate').setAttribute('value',date);");

There are two main techniques for doing this. One is string concatenation, the other is string interpolation.
Concatenation
var theDate = "8/16/2013";
var theCommand = "window.document.getElementById('pmtDate').setAttribute('value'," + theDate + ");"
executor.ExecuteScript(theCommand);
Interpolation
var theDate = "8/16/2013";
var theCommand = String.Format("window.document.getElementById('pmtDate').setAttribute('value', {0});", theDate);
executor.ExecuteScript(theCommand);
If you're using Selenium, you can also pass an argument array to the function:
var theDate = "8/16/2013";
var theCommand = "window.document.getElementById('pmtDate').setAttribute('value', arguments[0]);";
executor.ExecuteScript(theCommand, new object[] { theDate });

Try this:
var currentDate = new Date();
window.document.getElementById('pmtDate').setAttribute('value', getDate());
function getDate(){
return currentDate.toString();
}
Fiddle
Updated Answer:
executor.ExecuteScript("function getDate(){return currentDate.toString();}var currentDate = new Date();window.document.getElementById('pmtDate').setAttribute('value', getDate());");

Related

Combine date and time from string and convert to datetime

I have 2 strings (date(13/04/2021),time("06:30")) and I want to combine them together to datetime format and I am getting the below error.
System.FormatException: 'String was not recognized as a valid DateTime.'
What I am doing wrong?
if I change the tempdate format to "yyyy-dd-MM" I get the error there
var type = form["GymType"];
var time = form["states_ddl"];
var date = form["date2"];
var username = form["username"];
var numberofpersons = 0;
if (type == "2")
{
//gym
numberofpersons = 9;
}
else
{
//cross
numberofpersons = 5;
}
Booking toadd = new Booking();
var currenduser = User.Identity.GetUserId();
var tempdate = DateTime.ParseExact(date , "dd/MM/yyyy", CultureInfo.InvariantCulture);
DateTime d = DateTime.ParseExact(tempdate + " " + time, "yyyy-dd-MM HH:mm", CultureInfo.InvariantCulture);
What you do is convert de string to a dateTime 'tempdate', and than on the next line you convert it back to a string.
What i would do is convert the date as you do and parse the time separate, than add them together.
var tempdate = DateTime.ParseExact(date , "dd/MM/yyyy", CultureInfo.InvariantCulture);
var timspan = TimeSpan.Parse(time);
DateTime d = tempdate.Add(timspan);
You can use Convert to do this
var date = Convert.ToDateTime("13/04/2021 06:30");
try this
string d = "13/04/2021";
string t = "10:30PM";
string dateAndTime = d.Trim() + ' ' + t.Trim();
DateTime dt = DateTime.ParseExact(dateAndTime, "MM/dd/yyyy hh:mmtt",
CultureInfo.InvariantCulture, DateTimeStyles.None);
dt = DateTime.Parse(dateAndTime, CultureInfo.InvariantCulture,
DateTimeStyles.None);

Separate Date and time with " (String.Format)

Is it possible to separate Date and time with ".
So it would be:
"ddMMyyyy","HHmmss"
Right now i have:
DateTime dt = aPacket.dtTimestamp;
string d = dt.ToString("\"ddMMyyyy\",\"HHmmss\"");
and String.Format shows me just "ddMMyyyy,HHmmss"
Thank you everyone for helping me !!! But i will mark the first answer as the right one
You can try formatting:
DateTime dt = DateTime.Now;
// "01072016","101511"
string d = String.Format("\"{0:ddMMyyyy}\",\"{0:HHmmss}\"", dt);
" is a formatting character, so it needs to be escaped with \, e.g.
string d = dt.ToString("\\\"ddMMyyyy\\\",\\\"HHmmss\\\"");
You may find a verbatim string slightly more readable:
string d = dt.ToString(#"\""ddMMyyyy\"",\""HHmmss\""");
Custom Date and Time Format Strings (MSDN)
I would say like this:
var now = DateTime.Now;
var date = now.ToString("ddMMyyyy", CultureInfo.InvariantCulture);
var time = now.ToString("HHmmss", CultureInfo.InvariantCulture);
var dt = string.Format(CultureInfo.InvariantCulture, "\"{0}\",\"{1}\"", date, time);
Console.WriteLine(dt);
You can try this:
var now = DateTime.Now;
var formattedDateTime = $"{now.ToString("ddMMyyyy")},{now.ToString("HHmmss")}";

String which is not in datetime format to DateTime

I have a string which is not in a datetime format eg:20160503. How can i change it to Datetime. I tried using Substring. It is working.Is there any more efficient way? Below is what I have right now.
string a = "20160503";
int b = Convert.ToInt32(a.Substring(0, 4));
int c = Convert.ToInt32(a.Substring(4, 2));
int d = Convert.ToInt32(a.Substring(6, 2));
string date = b + "/" + c + "/" + d;
DateTime result = new DateTime();
DateTime.TryParse(date, out result);
Since you know the exact format of your datetime, you could try to use the ParseExact DateTime's method.
var dt = DateTime.ParseExact(a,"yyyyMMdd",CultureInfo.InvariantCulture);
For further info, please have a look here.
Try somthing like this:
Define your own parse format string to use.
string formatString = "yyyyMMdd";
string sample = "20160503";
DateTime dt = DateTime.ParseExact(sample,formatString,null);
Thanks for your replies. Finaly I ended up using DateTime.TryParseExact
string dateString = "20150503";
DateTime dateValue = new DateTime();
DateTime.TryParseExact(dateString, "yyyyMMdd", new CultureInfo("en-US"), DateTimeStyles.None, out dateValue);

passing to MVC Control parameter from Javascript

In my jQuery code I am calling a controller action and I'm trying to pass in dates from the fullcalendar plugin:
url: ('Home/List/?dateValueStart=' + new Date($('#calendar').fullCalendar('getView').start))
+ '&dateValueEnd=' + new Date($('#calendar').fullCalendar('getView').end),
In my controller I have my method setup like this:
public ActionResult List(String dateValueStart, String dateValueEnd)
When I debug dateValueStart I see this:
Tue Oct 1 00:00:00 MDT 2013
DateTime dateVal = Convert.ToDateTime(dateValueStart);
But when I try to convert this to a date it tells me it is invalid.
String was not recognized as a valid DateTime.
How can I get a date close to 10/1/2013?
Javascript date support isn't the greatest. There are shorter ways to do the following but they don't work in all browsers:
var formatDate = function(d){
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
return curr_month + "/" + curr_date + "/" + curr_year;
}
var startDate = new Date($('#calendar').fullCalendar('getView').start);
var endDate = new Date($('#calendar').fullCalendar('getView').end);
var url = 'Home/List/?dateValueStart=' + formatDate(startDate) + '&dateValueEnd=' +
formatDate(endDate)
Also, check out this

how to change time format to 24hrs? in c#

Literal four = new Literal();
string timeanddate;
timeanddate = DateTime.UtcNow.ToString();
DateTime dt = new DateTime();
DateTime dt_calc = new DateTime();
dt = Convert.ToDateTime(timeanddate);
dt_calc = dt.AddHours(3);
four.Text = "3hr added and this gives>> " + dt_calc.ToString();
form1.Controls.Add(four);
its all in AM PM i want to work with 24hrs
See this page for every way you could possibly want to format a DateTime.
Note that you use "HH" for 24-hour time.
For example, if you wanted the format "23:00:00" instead of "11:00:00 PM" you would use:
string formatted = dt_calc.ToString("HH:mm:ss");
By the way, your initialization of your DateTime values with new DateTime() is unnecessary.
You have to change the current culture.
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(1053);
string swedishTime = DateTime.Now.ToShortTimeString(); //24h format
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo(1033);
string englishTime = DateTime.Now.ToShortTimeString(); //am/pm format
System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortTimePattern = "HH:mm:ss"
Old post, but here is the syntax from above with string interpolation:
string formatted = $"{dt_calc:HH:mm:ss}";

Categories

Resources