int to string to date - asp.net

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
}

Related

how to convert any string formatted date to datetime variable.?

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);

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;
}

DATE BIND IN DROP DOWN LIST

This is Query which i can get all record which is fall between in both dates.
SELECT * FROM tblBooking WHERE
Convert(datetime,'2013-08-20 04:00:00.000') --start date
BETWEEN FromDateWithStartTime AND ToDateWithEndTime
OR Convert(datetime,'2013-08-30 04:00:00.000') --endDate
BETWEEN FromDateWithStartTime AND ToDateWithEndTime
or FromDateWithStartTime BETWEEN Convert(datetime,'2013-08-20 04:00:00.000') --startdate
AND Convert(datetime,'2013-08-30 04:00:00.000') --enddate
or ToDateWithEndTime BETWEEN Convert(datetime,'2013-08-20 04:00:00.000')--start date
AND Convert(datetime,'2013-08-30 04:00:00.000')
and in C# i an fetching start Date and End Date.
FromDateWithStartTime =
Convert.ToDateTime(dt.Rows[0]["Fdate"]).ToString("yyyy/MM/dd
HH:mm:ss");
ToDateWithEndTime = Convert.ToDateTime(dt.Rows[0]["Edate"]).ToString("yyyy/MM/dd
HH:mm:ss");
I want those date which is fall between START DATE AND END DATE.
AND BIND ALL DATES IN THE DRP DOWN LIST.
HOW I CAN DO IT.
you can just iterate through your query with a foreach loop and bind values to your dropdown
foreach (item in queryResults)
{
ddlDate.items.add(item);
}
You can bind dropdown using linq by querying datatable (dtData) you have populated
ddlDate.DataValueField = "date_value";
ddlDepartment.DataTextField = "date_value";
ddlDepartment.DataSource = (from row in dtData.AsEnumerable()
select new { date_value = row["date_Column"].ToString()}).Distinct().ToList();
ddlDate.DataBind();
You can use TimeSpan in C# using that you can get difference between these days. like when you substract one date from another using Date.Substract(AnotherDate) method it will return you Timespan. and you can get number of days between two days like this example:
DateTime StartDate = DateTime.Now;
DateTime EndDate= DateTime.Now.AddMonths(5);
TimeSpan DateDiff = EndDate.Subtract(StartDate);
int Days = DateDiff.Days;
string Date;
for (int i = 1; i <= Days; i++)
{
Date = StartDate.AddDays(double.Parse(i));
}
Try this.

string variable date to double in c#

In the following code ,i need to convert string to double. But the code doesn't work.
string fdate="7/4/2013";
double nextdate = Convert.ToDouble( fdate);
Try this..
DateTime ddd=Convert.ToDateTime("7/4/2013");
double dd = Convert.ToDouble(Convert.ToString(ddd.Month) + Convert.ToString(ddd.Day) + Convert.ToString(ddd.Year));
It will surely work
First convert date string you have to date using DateTime.ParseExact and use it to Convert.ToDouble or DateTime.ToOADate to convert it to double.
string fdate = "7/4/2013";
DateTime date = DateTime.ParseExact(fdate, "d/m/yyyy", System.Globalization.CultureInfo.InvariantCulture);
double nextdate = date.ToOADate();

To calculate days in a month

i have a textbox that displays date in format,for eg:March,20,2008.Then i need to get total days n march in year 2008.Can anybody help
You can use the DaysInMonth function
e.g.
Date.DaysInMonth(2008, 3)
Obviously, you'll have to pass the year and month to the function
int year = DateTime.Now.Year;
int month = DateTime.Now.Month;
int numDays = DateTime.DaysInMonth(year, month);
Use DataTime.DaysInMonth()
http://msdn.microsoft.com/en-us/library/system.datetime.daysinmonth(VS.71).aspx
Remember to use the date they entered.
VB Code:
dim enteredDate as DateTime = cdate(txtDateBox.text)
dim daysInMonth as Int = DateTime.DaysInMonth(enteredDate.year,enteredDate.month)

Resources