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.
Related
I have two parameters one for date and another for time, and i need date value part and time values part.
My two parameters are below.
// For Date parameter
DateTime dt = DateTime.ParseExact("01-jan-1999", "dd-MMM-yyyy", CultureInfo.InvariantCulture);
bo.Dateused5 = dt;
// For Time parameter
string Fromtiming = ddl_FromHours.SelectedItem.ToString() + ":" + ddl_FromMinutes.SelectedItem.ToString();
DateTime InterviewTime = Convert.ToDateTime(Fromtiming);//StartTime
bo.Dateused4 = InterviewTime;//InterviewTime
so i need to send mail to the candidate to only date part, should not contain time and time part, should not contain date.
are you looking for this:
DateTime dt = DateTime.ParseExact("01-jan-1999", "dd-MMM-yyyy", CultureInfo.InvariantCulture);
string mailDate = dt.ToString("dd-MMM-yyyy");// will give 01-jan-1999
string date = dt.ToString("dd-MM-yyyy"); // will give 01-01-1999
You can also try using String.Format()
string mailDate = String.Format("{0:dd-MM-yyyy}", dt); // will give 01-01-1999
You can use ToShortDateString():
DateTime dt = DateTime.ParseExact("01-jan-1999", "dd-MMM-yyyy", CultureInfo.InvariantCulture);
var date = dt.ToShortDateString();
Note that it uses date format attached to the current thread's culture info.
You would need to use strings rather than dates, so change the type of your variables to string so that
bo.Dateused5 = dt.ToString("dd-MMM-yyyy")
would set Dateused5 to a string of the date component, then
bo.Dateused4 = InterviewTime.ToString("HH:MM");
would set Dateused4 to the time component.
Couldn't test your code but I am very sure there are Functions "DateValue" and "TimeValue" you can make use of.
Something like,
Format(DateValue(any datetime), "dd-MM-yyyy")
gives you Only Date in the specified format. Similar way for TimeValue
I have a simple routine which parses a DateTime.Now & performs a .ToString() on it to add it into a file name to be saved:
DateTime timeNow = DateTime.Now;
string dateNow = timeNow.ToShortDateString();
DateTime dateTime = DateTime.ParseExact(dateNow, "dd/MM/yyyy", CultureInfo.InvariantCulture);
string DateString = dateTime.ToString("dd-MMM-yy");
string fileName = string.Concat("MyArticle_" + region + "_" + DateString + fileExtension);
this is the resulting output string:
MyArticle_Africa_07-May-15.PNG
This is all good until I get a user on an American machine where the DateTime settings are different e.g.
05-07-15
In this case my ParseExact() method throws an exception as the input is not a valid date time. Is there a way to accommodate all date time inputs & parse to dd/MM/YYYY?
Actually, you don't need all these lines of code. You just need this:
// We just have to pass to the ToString
// method the exact format we want. Under the hood the CLR has
// the know how to execute this command and you get the desired
// output.
string DateString = DateTime.Now.ToString("dd-MMM-yy");
Furthermore, we use the DateTime.ParseExact method, when we want to get this exception you have mentioned. Saying this, I mean that we know that the string representation of dates, which we want to parse are of the exact format, we have specified in DateTime.ParseExact and if some of them aren't we wan't to be informed know it. Usually, we would have a try catch clause and in the catch clause we log this.
You need to try this:
string DateString = DateTime.Now.ToString("dd-MMM-yy");
string fileName = String.Concat("MyArticle_" + region + "_" + DateString + fileExtension);
You don't even need to convert DateTime.Now to a string, you can create the entire string in one step using String.Format :
var fileName = String.Format("MyArticle_{0}_{1:dd-MMM-yy}{2}",
region,DateTime.Now,fileExtension);
or
var fileName = String.Format(CurrentInfo.InvariantCulture,
"MyArticle_{0}_{1:dd-MMM-yy}{2}",
region,DateTime.Now,fileExtension);
to avoid internationalization issues.
I am trying to combine a DateTime from a number of combo boxes that i have on a form.
From this image you can see how the combo boxes are laid out.
Wondering what is the best way to do this currently i have the following but am not sure that it is correct.
string startdate = cmbMonthYear.Text + "-" + cmbMonth.SelectedIndex.ToString()+ "-" + cmbDay.Text + " "+ "07:00";
DateTime StartDate = DateTime.ParseExact(startdate, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
What is the best way that i could go about this?
A better way would probably be to avoid the parse exact and make sure you have the most accurate representation of the date you want, preferably in integers. You'll want to set the Value-parts of the items in your comboboxes as well. You can probably do that in the code that adds the items to those comboboxes.
So you'll have something like:
// Check your input here
// ...
int day = Convert.ToInt32(cmbDay.SelectedValue);
int month = Convert.ToInt32(cmbMonth.SelectedValue); // No need to have text in SelectedValue, just integer
int year = Convert.ToInt32(cmbMonthYear.SelectedValue);
DateTime StartDate = new DateTime(year, month, day, 7, 0, 0);
This should work (if you're sure about your input):
var date = new DateTime(int.Parse(cmbMonthYear.Text), cmbMonth.SelectedIndex, int.Parse(cmbDay.Text), 7, 0, 0);
Use the DateTime.TryParse method to also validate the input from the user. A good practice when sometimes you have textboxes instead of dropdownlists:
string startdate = cmbMonthYear.SelectedValue
+ "-" + cmbMonth.SelectedValue
+ "-" + cmbDay.SelectedValue
+ " 07:00";
DateTime StartDate;
if(!DateTime.TryParse(startdate, out StartDate){
//invalid date, show a warning message (e.g. lblErrors.Text = "Start Date is not valid!";)
}else{
//your date is parsed and valid :)
}
I have asp.net Calendar to select the date 'selectedDate' and time Piker to select time 'starttime ' I am trying to add the 2 strings 1 startTime object 'startDateTime'
string strDate = Calendar1.SelectedDate.ToShortDateString(); ;
string startTime = txtb_endTimeManual.Text;
DateTime startDateTime = Convert.ToDateTime(strDate + startTime);
error message
String was not recognized as a valid DateTime.
You need to add a space between the two strings. If strDate is '1/15/2012' and startTime is '6:30:00 PM' then concatenating the two strings give you '1/15/20126:30:00 PM' so the format is all off.
DateTime startDateTime = Convert.ToDateTime(strDate + " " + startTime);
Without seeing the inputs it's hard to say whether you can trust they are formatted properly but, beyond that, I would say that you don't have a space between your date and time in the Convert.ToDateTime() method call.
Rather than doing a conversion to a string, a string concat, and another conversion, the Calendar already returns a DateTime object. You can set the time.
string startTime = txtb_endTimeManual.Text;
DateTime startDateTime = Calendar1.SelectedDate.Add(TimeSpan.Parse(startTime));
I am doing something to prepare a string to display in a format in server side but now I have to replace it in javascript so my server side code is:
DateTime now = DateTime.Now;
string date = now.GetDateTimeFormats('d')[0];
string time = now.GetDateTimeFormats('t')[0];
txtFileName.Value = someString.Length > 10 ? someString.Substring(0, 10).TrimEnd() + "_" + date + "_" + time : someString.TrimEnd() + "_" + date + "_" + time;
txtFileName.Value = txtFileName.Value.Replace(' ', '_');
How to achieve that?
Although JavaScript provides a bunch of methods for getting and setting parts of a date object, it lacks a simple way to format dates and times according to a user-specified mask.
Check these date function and follow following links:
Formatting a date in javascript
How can I convert string to datetime with format specification in JavaScript?
var d = new Date();
var datepart = d.getDate() + " " + d.getMonth() + " " + d.getFullYear();
using Date object same you can create time format also.
To create filename format use javascript replaceenter link description here method
var myNewString = myOldString.replace("username", visitorName);
Check this to create trim method or extend javascript String Object
Hope this is enough to convert your server side code in java script...
check out DateJS, it has very powerfull date manipulation functions