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;
}
}
Related
When I calculated the duration between 28/07/2018 and 01/08/2018, its result was two days. the correct answer is four days. what is my mistake?
my Code is :
private static double DateDurationCalculate(DateTime startTime, DateTime endTime)
{
TimeSpan span = endTime.Subtract(startTime);
return span.TotalDays;
}
startTime is 2018/07/28 11:54 and endTime is 2018/08/01 09:28.
Try like this,
DateTime startTime = DateTime.Parse("2018/07/28 11:54");
DateTime endTime = DateTime.Parse("2018/08/01 09:28");
private static double DateDurationCalculate(DateTime startTime, DateTime endTime)
{
startTime = new DateTime(startTime.Year, startTime.Month, startTime.Day, startTime.Hour, startTime.Minute, 0);
endTime = new DateTime(endTime.Year, endTime.Month, endTime.Day, endTime.Hour, endTime.Minute, 0);
TimeSpan span = endTime.Date.Subtract(startTime.Date);
return span.TotalDays;
}
Thanks to everyone, my problem was solved with the following method.
private static double DateDurationCalculate(DateTime startTime, DateTime endTime)
{
var usCulture = "en-US";
startTime = DateTime.Parse(startTime.ToString("MM/dd/yyyy"), new CultureInfo(usCulture, true));
endTime = DateTime.Parse(endTime.ToString("MM/dd/yyyy"), new CultureInfo(usCulture, true));
TimeSpan span = endTime.Date.Subtract(startTime);
return span.TotalDays;
}
Say I'm entering a date like DateTime.Now or Date.Now so My required output will be a list of dates in that entered date's week.
input = 1/1/2018
output needed as = {1/1/2018,2/1/2018,3/1/2018,4/1/2018,5/1/2018,6/1/2018,7/1/2018}
Dim today As Date = CDate(req.SelectedDate)
Dim currentDayOfWeek As Integer = CInt(today.DayOfWeek)
Dim sunday As DateTime = today.AddDays(-currentDayOfWeek)
Dim monday As DateTime = sunday.AddDays(1)
If currentDayOfWeek = 0 Then
monday = monday.AddDays(-7)
End If
Dim dates = Enumerable.Range(0, 7).[Select](Function(days) sunday.AddDays(days)).ToList()
You can use this method:
public static DateTime StartOfWeek(DateTime dt, DayOfWeek startOfWeek)
{
int diff = (7 + (dt.DayOfWeek - startOfWeek)) % 7;
return dt.AddDays(-1 * diff).Date;
}
static void Main(string[] args)
{
DateTime dt = DateTime.Now;
DateTime sow = StartOfWeek(dt, DayOfWeek.Monday);
DateTime[] allWeekDays = Enumerable.Range(0, 7).Select(d => sow.AddDays(d)).ToArray();
}
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'm trying to learn how to schedule pictures depending on what time a user has selected.
Here is code with questions:
private void startjob()
{
string theDate = DateTimePicker1.Value.ToString();
DateTime dt = Convert.ToDateTime(date);
{
DateTime start = new DateTime(2009, 12, 9, 10, 0, 0); //How Do I make this to read the string that is converted from DateTimePicker instead of this?
DateTime end = new DateTime(2009, 12, 10, 12, 0, 0); //How Do I make this to read the string that is converted from DateTimePicker instead of this?
DateTime now = DateTime.Now;
if ((now > start) && (now < end))
{
//match found
}
}
}
DateTimePicker.Value returns the DateTime object. You're trying to convert to and from a string type unnecessarily.
DateTime start = DateTimePickerStart.Value;
DateTime end = DateTimePickerEnd.Value;
Supposing your controls are named as DateTimePicker1 and DateTimePicker2:
private void startjob()
{
DateTime start = DateTimePicker1.Value;
DateTime end = DateTimePicker2.Value;
DateTime now = DateTime.Now;
if ((now > start) && (now < end))
{
//match found
}
}
DateTimePicker.Value property is a DateTime object itself, hence your code can be simplified, no need to convert to string.
private static DateTime FromMS(long microSec)
{
long milliSec = (long)(microSec / 1000);
DateTime startTime = new DateTime(1970, 1, 1);
TimeSpan time = TimeSpan.FromMilliseconds(milliSec);
DateTime v = new DateTime(time.Ticks);
DateTime result = new DateTime(startTime.Year + v.Year, startTime.Month + v.Month, startTime.Day + v.Day, startTime.Hour + v.Hour, startTime.Minute + v.Minute, startTime.Millisecond + v.Millisecond);
return result;
}
This result is wrong...
Why ???
You already have the result of the conversion to milliseconds when you do:
TimeSpan time = TimeSpan.FromMilliseconds(milliSec);
DateTime v = new DateTime(time.Ticks); //This is the result
If you want to add the milliseconds to UNIX time, then all you have to do is:
TimeSpan time = TimeSpan.FromMilliseconds(milliSec);
DateTime result = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
result = result.Add(time);
If the time isn't in UTC then omit the DateTimeKind.Utc part, but it's generally a good idea to keep the time in UTC and only convert to local time when needed.
private static DateTime FromMS(long microSec)
{
long milliSec = (long)(microSec / 1000);
DateTime startTime = new DateTime(1970, 1, 1);
TimeSpan time = TimeSpan.FromMilliseconds(milliSec);
return startTime.Add(time);
}
I use this method to convert from a Unix Epoch (with milliseconds) to a DateTime object
private static readonly DateTime UnixEpochStart =
DateTime.SpecifyKind(new DateTime(1970, 1, 1), DateTimeKind.Utc);
public static DateTime ToDateTimeFromEpoch(this long epochTime)
{
DateTime result = UnixEpochStart.AddMilliseconds(epochTime);
return result;
}
long ticks = new DateTime(1970, 1, 1).Ticks;
DateTime dt = new DateTime(ticks);
dt.AddMilliseconds(milliSec);
Try this.
TimeSpan time = TimeSpan.FromMilliseconds(1509359657633);
DateTime date = new DateTime(1970, 1, 1).AddTicks(time.Ticks);
This will convert milliseconds into a correct DateTime.
NOTE:- If you get the milliseconds from JS like Date.now() the millisecond you received here is for UTC. So when you convert to C# DateTime, you will get DateTime in UTC time