protected void ButtonPesquisar_Click(object sender, EventArgs e)
{
var macroController = new MacroController();
var itens = macroController.ObterTodos(new Entities.FiltroMacro()
{
//NumeroLocomotiva = TextBoxNumeroLocomotiva.Text,
DataInicio = DateTime.Parse(TextBoxDataInicio.Text + " " + TextBoxHoraInicio.Text),
DataFim = DateTime.Parse(TextBoxDataFim.Text + " " + TextBoxHoraFim.Text)
my code now works as follows: inform the values in textBox and returns me what is posted. But I'm wanting to return the current date and time of the system without I need to fill.
This is very simple, you could have just Googled this.
TextBoxHora.Text = DateTime.Today.ToString("g"); // Just the time
TextBoxData.Text = DateTime.Today.ToString("D"); // Just the Date
TextBoxDataHora.Text = DateTime.Today.ToString(); // Complete date and Time
TextBoxDataHoraCompleta.Text = DateTime.Now;
Go to these links for more information.
Link
Link
In the code I saw that you want the get the date from the text boxes, so: If you want to parse the date time and provide the format in one string you should use the DateTime.ParseExact method. For instance, see the usage in a sample console application:
static void Main(string[] args)
{
var parsedDate = DateTime.ParseExact("2014$05$01", "yyyy$MM$dd", DateTimeFormatInfo.CurrentInfo);
Console.WriteLine("Year: {0}",parsedDate.Year);
Console.WriteLine("Month: {0}",parsedDate.Month);
Console.WriteLine("Day: {0}",parsedDate.Day);
Console.ReadLine();
}
You can read Standard Date and Time Format Strings at msdn for searching more information about the date formats.
Related
I am attaching a .ics file with a email I send to the user. It works fine except the date and time. Currently I get system date and time when I double click on the attached .ics file. I couldn't add my own date columns value which I get from database to the calendar. Really appreciate some help.
Here is the contents for the .ics file:
string calLocation = eventRecordAfterInsert.location;
string calSubject = eventRecordAfterInsert.eventName;
string calDescription = "Event Schedule Description";
DateTime? calDate = eventRecordAfterInsert.eventDt;
DateTime? calTime = Convert.ToDateTime(row.Cells[11].Text);
DateTime calEventDateAndTime = calDate.Value.Date + calTime.Value.TimeOfDay;
String[] contents = { "BEGIN:VCALENDAR",
"VERSION:2.0",
"PRODID:-//flo Inc.//FloSoft//EN",
"METHOD:PUBLISH",
"BEGIN:VEVENT",
//"UID:{0}",
"DTSTAMP:" + calEventDateAndTime,
"DTEND:" + calEventDateAndTime,
"Location:"+ calLocation,
"Description;Encoding=QUOTED-PRINTABLE:" + calSubject,
"Summary:" + calDescription,
"Priority:3",
"BEGIN:VALARM",
"TRIGGER:-PT15M",
"ACTION:DISPLAY",
"DESCRIPTION:Reminder",
"END:VALARM",
"END:VEVENT", "END:VCALENDAR" };
System.IO.File.WriteAllLines(Server.MapPath("EventDetails.ics"), contents);
Date strings within an ICS file should be in the yyyyMMddTHHmmssZ format. So instead of:
"DTSTAMP:" + calEventDateAndTime,
You should do:
"DTSTAMP:" + calEventDateAndTime.ToUniversalTime().ToString("yyyyMMddTHHmmssZ"),
Or since you're repeating the value, move it out into another variable before appending it into your string array.
You are also trying to add a DateTime's time component to another DateTime with a date component, but I don't think it's going to work the way you want it to. You'll have to rethink that approach, perhaps parsing the time component out of the cell and adding that to the date:
DateTime? calDate = eventRecordAfterInsert.eventDt;
TimeSpan calTime = TimeSpan.Parse(row.Cells[11].Text);
DateTime calEventDateAndTime = calDate.Value.Date.Add(calTime);
Or combining the two DateTime structs into third new one:
DateTime calEventDateAndTime = new DateTime(calDate.Value.Year,
calDate.Value.Month,
calDate.Value.Day,
calTime.Value.Hour,
calTime.Value.Minute,
calTime.Value.Second);
Also, you aren't checking if calDate or calTime have values either, so I don't see the point in making them Nullable<DateTime>s.
I am creating my first win form application, written in C#. I have added a little bit of code to display the current date and time on the first tab page as below:
private void Form1_Load(object sender, EventArgs e)
{
// connect to database
c = new Connection();
connect.ConnectionString = c.getConnection();
//deals with date and time
Timer tmr = new Timer();
tmr.Interval = 1000;//ticks every 1 second
tmr.Tick += new EventHandler(tmr_Tick);
tmr.Start();
}
private void tmr_Tick(object sender, EventArgs e)
{
labeltime.Text = DateTime.Now.ToString(" Todays Date: dd/MM/yyyy\n\n Current Time: HH:mm:ss");
}
The output in not correct however. The date and time are correct and ticking away nicely but where I want it to display "Todays Date:" it is messy like " To15a1342 (42 being the seconds from the time, ticking away) and the "Current Time" reads CuRRenP Ti50e:
Does it matter that I am in the UK? Would this require me to use different code or something?
Your text is being interpreted as formatting strings, as explained in DateTime.ToString(string format):
The format parameter should contain either [...] a custom format pattern (see Custom Date and Time Format Strings)
Only unrecognized characters are printed as-is, but as you noticed for example the s gets replaced by the value of Seconds.
To let ToString() ignore your text, you need to escape the literals, preferably with single quotes (') (the alternative is a backslash in front of each literal):
string dateTimeString = DateTime.Now
.ToString("' Todays Date: 'dd/MM/yyyy'\n\n Current Time: 'HH:mm:ss");
Or build up the string from separate parts:
var now = DateTime.Now;
string dateTimeString = "' Todays Date: ";
dateTimeString += now.ToString("dd/MM/yyyy");
dateTimeString += "\n\n Current Time: ";
dateTimeString += now.ToString("HH:mm:ss");
Above string concatenation example can in turn be simplified as #Rohit's answer demonstrates.
Use String.Format like this:
string dateTime = String.Format("Todays Date: {0}, Current Time: {1}",
DateTime.Now.ToString("dd/mm/yyyy"),
DateTime.Now.ToString("hh:mm:ss"));
I want to make that code to show me a current datetime on my pc clock when i click a button... i did it with that code listed down. But here is my question how can i make the "ddd" (day of a week) to be shown in number, not in words, I mean:
00-sunday
01-monday
and etc. ...
This is my code for the button:
private void SetClock_Click(object sender, EventArgs e)
{
SetClock.Click += new EventHandler(SetClock_Click);
{
DateTime time = DateTime.Now;
string format = "yy-MM-dd-ddd-hh-mm-ss";
txtSend.Text = time.ToString(format);
}
}
What i have to add to make it. Thanks
txtSend.Text = string.Format("{0:yy-MM-dd}-{1:00}-{0:hh-mm-ss}", time, (int)time.DayOfWeek);
A format isn't required, you can just cast DayOfWeek to an int:
var dayAsInt = (int)DateTime.Now.DayOfWeek;
Looking at the Custom Date and Time Format Strings page, there doesn't appear to be a format string for it :-(
The following should do it:
txtSend.Text =
time.ToString("yy-MM-dd-") +
((int)time.DayOfWeek).ToString("00") +
time.ToString("-hh-mm-ss");
You could use this to get the day of week numeric
int dayofweek = (int)DateTime.Now.DayOfWeek;
My problem is to delete data from given date to current date,
My code works fine for deleting the data by the date given in the DateTimePicker.
i want to delete the data from given date in DateTimePicker to CurrentDate.
For example:
In subfolder123 the data is available from 20100131 to 20110531 (Date Format yyyyMMdd).
I want to delete the date from 20100215 to 20110531.
Hope you understood my Question and problem.
Is there any suggestions?
Here is my code:
private void button1_Click(object sender, EventArgs e)
{
string todaysDate = dateTimePicker1.Text;
int FinalDate4 = 0;
string Destinationnsefx = "C:\\folder\\subfolder\\subfolder123";
int xyz = 0;
string SecSym = (9722).ToString();
MWriterClass writerdelete1 = new MWriterClass();
try
{
writerdelete1.OpenDirectory(Destinationnsefx);
writerdelete1.OpenSecurityBySymbol(SecSym);
FinalDate4 = int.Parse(todaysDate);
if (writerdelete1.get_bDateExists(FinalDate4))
{
try
{
writerdelete1.DeleteIntradaySecRecordEx(FinalDate4, 080000, 240000);
}
catch
{
}
}
writerdelete1.CloseSecurity();
writerdelete1.CloseDirectory();
}
catch
{
}
}
Thanks in advance.
It sounds like you get a date from your DateTime picker and you need to work from that start date to your end date, correct?
In that case, look at the AddDays method for a DateTime object.
For example, this snippet of code will start at 4/1/2011 and print every date from then until today, in the format you specified.
var workingDate = new DateTime(2011, 4, 1);
while (workingDate < DateTime.Today)
{
workingDate = workingDate.AddDays(1);
Console.WriteLine(string.Format("{0:yyyyMMdd}", workingDate));
}
How to get the selected date of a MonthCalendar control in C# (Window forms)
"Just set the MaxSelectionCount to 1 so that users cannot select more than one day. Then in the SelectionRange.Start.ToString(). There is nothing available to show the selection of only one day." - Justin Etheredge
From here.
I just noticed that if you do:
monthCalendar1.SelectionRange.Start.ToShortDateString()
you will get only the date (e.g. 1/25/2014) from a MonthCalendar control.
It's opposite to:
monthCalendar1.SelectionRange.Start.ToString()
//The OUTPUT will be (e.g. 1/25/2014 12:00:00 AM)
Because these MonthCalendar properties are of type DateTime. See the msdn and the methods available to convert to a String representation. Also this may help to convert from a String to a DateTime object where applicable.
Using SelectionRange you will get the Start and End date.
private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
{
var startDate = monthCalendar1.SelectionRange.Start.ToString("dd MMM yyyy");
var endDate = monthCalendar1.SelectionRange.End.ToString("dd MMM yyyy");
}
If you want to update the maximum number of days that can be selected, then set MaxSelectionCount property. The default is 7.
// Only allow 21 days to be selected at the same time.
monthCalendar1.MaxSelectionCount = 21;
For those who are still trying, this link helped me out, too; it just puts it all together:
http://dotnetslackers.com/VB_NET/re-36138_How_To_Get_Selected_Date_from_MonthCalendar_control.aspx
private void MonthCalendar1_DateChanged(object sender, System.Windows.Forms.DateRangeEventArgs e)
{
//Display the dates for selected range
Label1.Text = "Dates Selected from :" + (MonthCalendar1.SelectionRange.Start() + " to " + MonthCalendar1.SelectionRange.End);
//To display single selected of date
//MonthCalendar1.MaxSelectionCount = 1;
//To display single selected of date use MonthCalendar1.SelectionRange.Start/ MonthCalendarSelectionRange.End
Label2.Text = "Date Selected :" + MonthCalendar1.SelectionRange.Start;
}
It'll be helpful if you want just to convert it by:
String myCalendar = monthCalendar1.SelectionRange.Start.ToShortDateString()
But if you want to get a formatted output you could instead:
String myCalendar = monthCalendar1.SelectionRange.Start.ToString("yyyy-MM-dd")
It's important to use year and day as lower caps, and month as upper or else it'll return you a wrong format, for example, if you do:
String myCalendar = monthCalendar1.SelectionRange.Start.ToString("YYYY-MM-DD")
it will return: YYYY-07-DD (If the original date's month was July)
private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
{
string clickeddate = monthCalendar1.SelectionRange.Start.ToString("dddd, dd MMM yyyy");
richTextBox.AppendText(clickeddate); //or whatever you decide to do with it.
}
SelectionRange property