how to convert any string formatted date to datetime variable.? - asp.net

domaindate.Text="31-03-3015";
DateTime dt = DateTime.Parse(domaindate.Text);
int day = dt.Day;
int month = dt.Month;
int year = dt.Year;
if (ddlyear.SelectedItem.Text == "1")
{
year = year + 1;
month = month - 1;
edate = String.Join("/", day, month, year);
}
p.expirydate = Convert.ToDateTime(edate);
Where p.expiredate id DateTime Property variable.
Geting Error:String was not recognized as a valid DateTime.
So, How i convert it to dd/MM/yyyy.?

Use the ParseExcact Method with the string format.
p.expirydate =DateTime.ParseExact(edate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
EDIT:
If you want add days to a specific date use the AddDays() Method
DateTime dt= DateTime.Parse(domaindate.Text);
p.expirydate = today.AddDays(number of days you want);

Related

Get week of month with Joda-Time

Is it possible to parse a date and extract the week of month using Joda-Time. I know it is possible to do it for the week of year but I cannot find how/if it is possible to extract the week of month.
Example: 2014-06_03 where 03 is the third week of this month
DateTime dt = new DateTime();
String yearMonthWeekOfMonth = dt.toString("<PATTERN for the week of month>");
I have tried the pattern "yyyyMMW" but it is not accepted.
Current joda-time version doesn't support week of month, so you should use some workaround.
1) For example, you can use next method:
static DateTimeFormatter FORMATTER = DateTimeFormat.forPattern("yyyy-MM_'%d'");
static String printDate(DateTime date)
{
final String baseFormat = FORMATTER.print(date); // 2014-06_%d
final int weekOfMonth = date.getDayOfMonth() % 7;
return String.format(baseFormat, weekOfMonth);
}
Usage:
DateTime dt = new DateTime();
String dateAsString = printDate(dt);
2) You can use Java 8, because Java's API supports week of month field.
java.time.LocalDateTime date = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM_W");
System.out.println(formatter.format(date));
This option in Joda is probably nicer:
Weeks.weeksBetween(date, date.withDayOfMonth(1)).getWeeks() + 1
For the case you don't like calculations so much:
DateTime date = new DateTime();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date.toDate());
int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
If the start day of week is Monday then you can use it:
public int getWeekOfMonth(DateTime date){
DateTime.Property dayOfWeeks = date.dayOfWeek();
return (int) (Math.ceil((date.dayOfMonth().get() - dayOfWeeks.get()) / 7.0)) + 1;
}

pass in date and get first and last date of that month?

I have a date stored in a session variable, I then convert it to a date. Now I want to get the first and last date of the month for the date that is passed.
DateTime tempDate = System.Convert.ToDateTime(Session["Diary_Date"]);
DateTime startOfMonth = //what goes here?
DateTime endOfMonth = //??
One way to do this:
DateTime startOfMonth = new DateTime(tempDate.Year, tempDate.Month, 1);
DateTime endOfMonth = startOfMonth.AddMonths(1).AddDays(-1);
Another way is
DateTime startOfMonth = new DateTime(tempDate.Year, tempDate.Month, 1);
DateTime endOfMonth = new DateTime(tempDate.Year, tempDate.Month, DateTime.DaysInMonth(tempDate.Year, tempDate.Month));

Difference in years,months and days between 2 hijri dates

Please can anyone provide a method to calculate the difference between 2 hijri dates thanks in advance
i tried this code
HijriCalendar hijriCal=new HijriCalendar();
DateTimeFormatInfo DTFormat = new System.Globalization.CultureInfo("ar-sa", false).DateTimeFormat;
DTFormat.Calendar = new System.Globalization.HijriCalendar();
DTFormat.ShortDatePattern = "dd/MM/yyyy";
string HijriDate = FromDate.Date.ToString("d", DTFormat);
string[] fromDateParams=HijriDate.Split('/');
HijriDate = ToDate.Date.ToString("d", DTFormat);
string[] toDateParams = HijriDate.Split('/');
DateTime fromDateHijri = new DateTime(hijriCal.GetYear(FromDate), hijriCal.GetMonth(FromDate), int.Parse(fromDateParams[0]), hijriCal);
DateTime toDateHijri = new DateTime(hijriCal.GetYear(ToDate), hijriCal.GetMonth(ToDate), int.Parse(toDateParams[0]), hijriCal);
TimeSpan ts = ToDate.Subtract(FromDate);
As long as you just store the dates in normal datetimes, just treat them as any other date.
public TimeSpan GetDifference(this DateTime date1, DateTime date2) {
if (date1 < date2) {
return date2 - date1;
}
else if (date1 > date2) {
return date1 - date2;
}
return new TimeSpan(0);
}

how can i insert date in yyyy-mm-dd format into flex

I have to select date from date field in flex and store that date into SqlLite database. I
am inserting date by using this code :
dbInsertDate = datechooser.selectedDate.getFullYear().toString()+'0'+(datechooser.selectedDate.getMonth()+1).toString()+"-0"+datechooser.selectedDate.getDate().toString();
where dbInsertDate is string type variable and datechooser is date field id. It stores the date in database in a format like 2455361.5. I want to store date in either dd-mm-yyyy format or yyyy-mm-dd format in the database. How can i store date in yyyy-mm-dd format in SqlLite using flex ?
Thanks
You can use a DateFormatter:
var formatter:DateFormatter = new DateFormatter();
formatter.formatString = "DD-MM-YYYY";
var result:String = formatter.format(datechooser.selectedDate);
If you use localization you can "translate" the format string to the needed format.
solution: Convert date to string and in yyyy mm dd format
code:
here date will be like this string 2011-02-15
enter code heredateChooser is datefield.
public var dbInsertDate:String;
private var selectedDateByUser:int;
if(dateChooser.selectedDate.getMonth() < 9)
{
if(dateChooser.selectedDate.getDate() < 10)
{
dbInsertDate =
dateChooser.selectedDate.getFullYear().toString()+'-0'+(dateChooser.selectedDate.getMonth()+1).toString()+"-0"+dateChooser.selectedDate.getDate().toString();
}
else
{
dbInsertDate =
dateChooser.selectedDate.getFullYear().toString()+'-0'+(dateChooser.selectedDate.getMonth()+1).toString()+"-"+dateChooser.selectedDate.getDate().toString();
}
else
{
if(dateChooser.selectedDate.getDate() < 10)
{
dbInsertDate =
dateChooser.selectedDate.getFullYear().toString()+'-'+(dateChooser.selectedDate.getMonth()+1).toString()+"-0"+dateChooser.selectedDate.getDate().toString();
}
else
{
dbInsertDate =
dateChooser.selectedDate.getFullYear().toString()+'-'+(dateChooser.selectedDate.getMonth()+1).toString()+"-"+dateChooser.selectedDate.getDate().toString();
}
}
here date will be like this string 20110215. so you can store date in this format at varchar column type in sqqlite.
enter code here
var dateArray:Array = new Array();
dateArray = dbInsertDate.split('-');
selectedDateByUser = dateArray[0]+dateArray[1]+dateArray[2];

int to string to date

i use asp.net
i have a int (10112009)
in the end i want a date formate like day- day / month month / yyyy
what's the best way (or a way) to do that?
thanks
I would do something like this:
int n = 10112009;
DateTime date;
if (DateTime.TryParseExact(n.ToString("00000000"), "ddMMyyyy",
CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
// use date
}

Resources