I have a textfield that has a date with the format "12/23/2010".Is there away for me to get the number 23 using watin ie get number from textfield;i'm gonna use it like this.
1.Get datetime 12/23/2010 and get number '23'
2.substract 2 from 23 and store it somewhere[ie: 23 - 2 = 21]
3.Insert the new datetime number [ie:12/21/2010 ]
string myDate = browser.TextField(Find.ByName("myTextField")).Value;
DateTime time = = new DateTime();
time2 = time - 2;
browser.TextField(Find.ByName("myTextField")).TypeText(time2);
Is this possible?or should i be looking to another way.Ask the user to insert the data instead.
Try this :
string myDate = browser.TextField(Find.ByName("myTextField")).Value;
DateTime time = = new DateTime();
if(DateTime.TryParse(myDate, out time);) {
Console.WriteLine(time.Month);
}
else {
//Not a valid date.
}
Related
I need to get Date only given I have string day name, So far I have tried this
var now = DateTime.Now.Date;
var currentDay = now.DayOfWeek;
int days = (int)currentDay+1;
dt1 = now.AddDays(-days);
dt1 = dt1.AddHours(hour);
example "Tuesday". Get todays date bc today is tuesday
But no luck
base on
C#: DateTime.DayOfWeek to string comparison - Stack Overflow
c# - How can I get the DateTime for the start of the week? - Stack Overflow
DateTime getDate(string dayOfWeekString)
{
var dayOfWeek = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), dayOfWeekString);
var now = DateTime.Now.Date;
var diff = (7 + (now.DayOfWeek - dayOfWeek)) % 7;
return now.AddDays(-1 * diff).Date;
}
Console.WriteLine(getDate("Tuesday"));
Console.ReadLine();
For example, if the textbox contains 11/11/2016 OR 01/05/2016
How to get the day value only? example. 11 OR 01
One way is to parse the string in the TextBox to DateTime using an exact format. Then using the DateTime object, you can extract the Day Property:
DateTime date;
bool success = DateTime.TryParseExact(textBoxDate.Text, "dd/MM/yyyy",
CultureInfo.CurrentCulture,
DateTimeStyles.AssumeLocal, out date);
int day;
if(success)
{
day = date.Day; // 1
// or string stringDay = date.ToString("dd"); to get 01
}
else
{
// handle error
}
Another way that I don't prefer (Not 100% safe input validation) is to use String.Split like this:
string strDay = textBoxDate.Text.Split('/').FirstOrDefault();
int day;
if(Int32.TryParse(strDay, out day))
{
// success
}
else
{
// Handle Error
}
string myString = textbox1.text;
DateTime day = DateTime.ParseExact(myString, "dd/MM/yyyy", CultureInfo.InvariantCulture);
int day = birthday.Day;
Hei,
I want to add to a day wrote in a unix format a day or two.
I use DateTime.Now converted to Unix.The idea is if the order is finished before 16:00 the order will be shipped on next working day (datetime.now)+1. If the order is finished after 16:00 the order will be shipped on (datetime.now)+2.
This will be only on working day. If is weekend next working day will be on Monday.
DateTime normalSF;
MyDateTime normal = new MyDateTime();
MyDateTime normal1 = new MyDateTime();
date_ship_form = normal.ConvertToUnix(DateTime.Now);
normalSF = normal1.ConvertToDate(Convert.ToInt64(date_ship_form));
lblDateSF.Text = ((normalSF.Day < 10) ? "0" : "") + normalSF.Day.ToString() + "/" + ((normalSF.Month < 10) ? "0" : "") + normalSF.Month.ToString() + "/" + normalSF.Year.ToString();
This is how I use DateTime Unix in my code.
You can try this
var currdate = DateTime.Now;
long myStartDate = currdate.ToTimeStamp();
var resdate = myStartDate.AddDays(2);
long finaldate = resdate.ToTimeStamp();
You can try to convert the Unix timestamp to DateTime like this:
public static DateTime myDate(double unixTime)
{
System.DateTime dt = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
return dt.AddSeconds(unixTime).ToLocalTime();
}
I am trying to parse out a RadCalendar Date and disable the dates prior to our Start Date of an event.
We get our StartDateTime from a database and I would like to disable the dates from our Future StartDateTime all the way back to the beginning of the current (this) month.
EDIT: More specific
Example: My StartDateTime is in November 2014 but I want to disable all dates from that future date until back to the beginning of this current month (this month is August 2014).
Below is the code we currently have, but it is only looking back i < 31. This is why I would like to the DateTime get the number of days as an int all the way back to the beginning (the 1st) of the current month.
if (nextAvailableTime != null && nextAvailableTime.StartDateTime > DateTime.Today)
{
//DISABLE dates prior to next available date
DateTime dt = nextAvailableTime.StartDateTime.AddDays(-1);
for (var i = 0; i < 31; i++) //Would like to change this to beginning of current month.
{
tkCalendar.SpecialDays.Add(new RadCalendarDay(tkCalendar) { Date = dt.Date.AddDays(i * -1), IsDisabled = true, IsSelectable = false });
}
}
Why not subtract the 2 dates and get the difference in days? I used my own variable because I was unclear what your variables were. My loop is disabling going forward instead of multiplying by -1. You may need to edit the loop to be <= or start from 1 depending on if you want the first and last date to be included.
if (nextAvailableTime != null && nextAvailableTime.StartDateTime > DateTime.Today)
{
//DISABLE dates prior to next available date
DateTime currentDate = DateTime.Now;
DateTime futureDate = DateTime.Now.AddMonths(3);
int daysBetween = (futureDate - currentDate).Days;
for (var i = 0; i < daysBetween; i++)
{
tkCalendar.SpecialDays.Add(new RadCalendarDay(tkCalendar) { Date = currentDate.AddDays(i), IsDisabled = true, IsSelectable = false });
}
}
The answer we came up with was to get the next available date and then the beginning date of the current month and get the difference using DayOfYear.
Solution is below:
if (nextAvailableTime != null && nextAvailableTime.StartDateTime > DateTime.Today)
{
//DISABLE dates prior to next available date
DateTime dt = nextAvailableTime.StartDateTime.AddDays(-1);
DateTime nextDate = nextAvailableTime.StartDateTime;
//Gate the calendar to just go get the product's next available date and then get block out everything until the beginning of the current month.
var now = DateTime.Now;
var startOfMonth = new DateTime(now.Year, now.Month, 1);
TimeSpan daysBetween = (futureDate - startOfMonth);
// for (var i = 0; i < 31; i++)//Original from 31 days from next available.
for (var i = 0; i < daysBetween.Days; i++) //Get difference between next available and beginning of current month.
{
tkCalendar.SpecialDays.Add(new RadCalendarDay(tkCalendar) { Date = dt.Date.AddDays(i * -1), IsDisabled = true, IsSelectable = false });
}
}
How do I covert JavaScript string "5:00 PM" to DateTime or TimeSpan when it get send to the MVC controller. I am using
bootstrap-timepicker
// usage
<script type="text/javascript">
$('#timepicker1').timepicker();
</script>
Javascript payload
{
Skip: 0
Status: []
Take: 15
DueTime: "1:00 PM" // keep in mind that this is a string
}
Server Object would be something like
class TimeSheet
{
public TimeSpan DueTime;
}
Use DateTime.Parse. Convert on server(on controller) when your string would transmit with your time.
http://msdn.microsoft.com/ru-ru/library/system.datetime.parse(v=vs.110).aspx
Okay so I read everyhting wrong hence the deleted answer.. !
But I'm not giving up ;)
Your bootstrap-timepicker, will give you a time as this "1:00 PM".
But before that we are going to look on the serverside to see what we can parse into a datetime object.
This is C# for parsing datetime.
string dateString, format;
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;
dateString = "15/08/2000 16:58"
format = "dd/MM/yyyy HH:mm"
result = DateTime.ParseExact(dateString, format, provider);
Now as you se your string wont look like that I'm going to assume that you want todays date!
This is function I tend to use most of the times when converting, to 24H clock.
function ConvertTimeformat(str) {
var time = str;
var hours = Number(time.match(/^(\d+)/)[1]);
var minutes = Number(time.match(/:(\d+)/)[1]);
var AMPM = time.match(/\s(.*)$/)[1];
if (AMPM == "PM" && hours < 12) hours = hours + 12;
if (AMPM == "AM" && hours == 12) hours = hours - 12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if (hours < 10) sHours = "0" + sHours;
if (minutes < 10) sMinutes = "0" + sMinutes;
//Creating the todays date in the right format
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;//January is 0!`
var yyyy = today.getFullYear();
if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm}
var todaysdate = dd+'/'+mm+'/'+yyyy +" " ; //<--I added an extra space!
var hoursNminutes = sHours + ":" + sMinutes
//CREATE THE RIGHT FORMAT FOR DATE.PARSEXACT "dd/MM/yyyy HH:mm"
var dateToParse = todaysdate + hoursNminutes
return dateToParse;
}
To Use the function do like this!
//Call it and provide your bootstrap time. And make it return to a variable
var dateToBeSentToServer = ConvertTimeformat("1:00 PM");
//OR With the bootstraptime as a variable
var dateToBeSentToServer = ConvertTimeformat(timevariable);
Now you can send dateToBeSentToServer to your serverside for parsing!