private void btntest_Click(object sender, EventArgs e)
{
DateTime d = new DateTime(2016,2,13);
LunarDate ld1 = LunarYearTools.SolarToLunar(d);
lblamlich.Text = (ld1.ToString());
}
Note DateTime d = new DateTime(Int year,Int month,Int day)
How to insert datetimepicker's value into new DateTime(2016,2,13) ???
I tried use casting datetime to Int but it didn't work:
string a = dateTimePicker1.Value.ToString();
int b = (int)(a);
DateTime d = new DateTime(b);
LunarDate ld1 = LunarYearTools.SolarToLunar(d);
lblamlich.Text = (ld1.ToString());
If I have understood you correctly, it should be as simple as this:
var d = dateTimePicker1.Value;
var newD = new DateTime(d.Year, d.Month, d.Day);
The DateTimePicker.Value property is already a DateTime. Just reference it:
DateTime d = dateTimePicker1.Value;
If you're only interested in the date and wish to ignore the time, use the DateTime.Date property:
DateTime d = dateTimePicker1.Value.Date;
Related
The Code A is from a sample project, I'm very strange why I will get a TimeSpan when a DateTime var minus another DateTime var ?
I think the DateTime var will be return when a DateTime var minus another DateTime var, just like Code B. You know that Code C is so.
Code A
DateTime dt1 = DateTime.FromOADate(lastVisted);
DateTime dt2 = DateTime.Now;
TimeSpan difference = dt2 - dt1;
Code B
DateTime dt1 = DateTime.FromOADate(lastVisted);
DateTime dt2 = DateTime.Now;
DateTime difference = dt2 - dt1;
Code C
int a = 5;
int b = 4;
int c = a-b;
A TimeSpan will be returned when subtracting one DateTime from another DateTime as that is how the subtraction operator is defined for DateTime.
I want to convert a double value into a date
String firstdate =startdate.Text;
String lastdate = enddate.Text;
DateTime enteredDate1 = DateTime.Parse(firstdate);
DateTime enteredDate2 = DateTime.Parse(lastdate);
double stdate= enteredDate1.ToOADate();
double endate = enteredDate2.ToOADate();
sliderStartLabel.Text = "Year" + firstdate + "to";
slider_TextBox_SliderExtender.Minimum = stdate;
sliderEndLabel.Text = lastdate;
slider_TextBox_SliderExtender.Maximum=endate;
DateTime myDate = DateTime.FromOADate(stdate);
String date = myDate.ToString("dd/MM/yyyy");
slider_TextBox.Text = date;
I expect the output as yyyy/mm/dd format but I got the output as a double value(ex:- 3456)
If you are work with Unix timestamp like calendar contract in Xamarin.android then this code will work fine.
class Program
{
static void Main(string[] args)
{
double d = 3456;
long l= Convert.ToInt64(d);
DateTime dt = new DateTime().AddMilliseconds(l);
Console.WriteLine(dt);
DateTime epoch = new DateTime(1970,1,1,0,0,0,0).AddMilliseconds(l);
Console.WriteLine(epoch);
DateTime now = DateTime.Now;
Console.WriteLine(now);
Console.WriteLine("Datetime");
Console.ReadLine();
}
protected long GetDateTimeMS(DateTime dt)
{
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime();
return (long)(dt - epoch).TotalMilliseconds;
}
}
I try to do little program, to convert DateTime to Double and from double to DateTime.
I used DateTimePicker in C#, to make my users choose date and time to convert, and it works great, but I have little problem with converting double value to DateTime.
When I have double value, I can easilly convert it to Date by using something like this var dt = DateTime.FromOADate(doubleDate); and var date = dt.Date; but I have no idea, how to convert this double value to time.
This is my code:
private void button2_Click(object sender, EventArgs e)
{
var doubleDate = Convert.ToDouble(textBox1.Text);
var dt = DateTime.FromOADate(doubleDate);
var date = dt.Date;
this.dateDateTime.Value = date;
}
Example value: 42722,7819696875 or 42710,5736342593
You need to set the format on the DateTimePicker, Try the following:
var d = 42722.7465005671d; // 12/18/2016 5:54:57 PM
var dateTime = DateTime.FromOADate(d);
this.dateDateTime.Value = date;
this.dateDateTime.Format = DateTimePickerFormat.Time;
I have different datetime object in my code.
I want to do something like that :
class Program
{
public static void Main()
{
DateTime date1 = new DateTime();
DateTime date2 = new DateTime();
DateTime date3 = new DateTime();
DateTime date4 = new DateTime();
DateTime date5 = new DateTime();
DateTime date6 = new DateTime();
DateTime date7 = new DateTime();
DateTime date8 = new DateTime();
DateTime date9 = new DateTime();
DateTime date10 = new DateTime();
string date = "";
for (int i = 1; i < 11; i++)
{
date + i = "2010-10-28 11:00"; //It's wrong just for imagine my case
}
}
}
In my loop, I want to assign the object date1 at a specific value.
Then date2, date3 etc... Until date10.
I know an equivalent method "FindControl" on web but it's not existing apparently for an application.
Have you an idea to resolve my problem ?
Thanks
I would use an array:
var dates = new DateTime[10];
for (int i = 0; i < 10; i++)
{
dates[i] = DateTime.Parse("2010-10-28 11:00");
}
And access it with index:
dates[i - 1]
if i >= 1 && i <= 10
I made a JavaScript for date of birth, when debugging you choose your day of birth in three drop down boxes. One for days one for months and one for years. I'm working on C# asp.net. The problem is when i click on test(which is submit or confirm) the date is not taken to the database table. It fills empty! Any help would be appreciated..
here's the code:
DateOfBirth.js:
function date_populate(dayfield, monthfield, yearfield)
{
var today = new Date();
var dayfield = document.getElementById(dayfield);
var monthfield = document.getElementById(monthfield);
var yearfield = document.getElementById(yearfield);
for (var i = 0; i < 32; i++)
{
dayfield.options[i] = new Option(i , i + 1)
dayfield.options[today.getDate()] = new Option(today.getDate(), today.getDate(), true, true)
}
for (var m = 0; m < 12; m++)
{
monthfield.options[m] = new Option(monthtext[m], monthtext[m])
monthfield.options[today.getMonth()] = new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true)
}
var thisyear = today.getFullYear()
for (var y = 0; y < 100; y++)
{
yearfield.options[y] = new Option(thisyear, thisyear)
thisyear -= 1
}
yearfield.options[0] = new Option(today.getFullYear(), today.getFullYear(), true, true)
}
Form.asp.cs
protected void Button1_Click(object sender, EventArgs e)
{
String D, M, Y, Full;
D = Day.Value.ToString();
M = Month.Value.ToString();
Y = Year.Value.ToString();
Full = D + "/" + M + "/" + Y;
}
Don't construct dates as strings. Pass them as dates. For example:
DateTime dob = new DateTime(Year.Value, Month.Value, Day.Value);
...
cmd.Parameters.AddWithValue("dob", dob);
where the cmd.CommandText involves #dob when you want to refer to the date of birth.
Format the date 'yyyy-mm-dd' and it will insert.
Are you using a stored procedure?
Change your SP parameter from SqlDbType.NVarChar to SqlDbType.DateTime and then pass the date as a DateTime object to the parameter. You dont need to worry about conversions anymore.
DateTime dateofbirth = new DateTime(Convert.ToInt32(yearfield.SelectedValue),Convert.ToInt32(monthfield.SelectedValue), Convert.ToInt32(dayfield.SelectedValue));
param[0] = new SqlParameter("#DateOfBirth", SqlDbType.DateTime);
param[0].Value = dateofbirth;