asp.net how to combine two datetime date to one datetime object? - asp.net

I have
Datetime starttime='1/1/1900 6:00:00PM'
Datetime ExamDate='12/9/2013 12:00:00PM'
i want to combine these such that the result
Datetime combine=starttime+ExamDate;
so,
Result Datetime combine='12/9/2013 6:00:00PM'
Please help thank you

I'm not 100% certain if this is what you're trying to achieve, but I'll give it a shot. To add time to an existing DateTime object, use the TimeSpan class. Ex:
TimeSpan StartTime = new TimeSpan(0, 6, 0, 0); //new time span of 6 hours
// create date time 2013-09-12 12:00
DateTime ExamDate = new DateTime(2013, 9, 12, 12);
//Add the 6 hour time span to your exam date to get combined date
DateTime Combined = ExamDate + StartTime;

If you'd simply like to add the TIME portion of starttime to examDate
var startTime= DateTime.Parse("1/1/1900 6:00:00PM");
var examDate = DateTime.Parse("12/9/2013 12:00:00PM");
var result = examTime.Add(starttime.TimeOfDay);
This will result in: 13/9/2013 6:00:00AM.
It's occurred to me you might just be trying to store the date in examDate, in which case setting examDate to 12/9/2013 12:00:00AM will give: 12/9/2013 6:00:00PM.

Related

Dart/Flutter get first DateTime of this week [duplicate]

This question already has answers here:
How to get start of or end of week in dart
(8 answers)
Closed 1 year ago.
Using flutter on android I am trying to get the first date of current week. For example today is 13/7/2020, assuming week starts from Saturday the first date of current week will be 11/7/2020.
This for example to get first date of month
DateTime firstDay = new DateTime(
DateTime.now().year,
DateTime.now().month,
1,
); //get first date this month
The end goal is to get timestamp of that date and fetch entries from sqflite database that is higher than that.
If Sunday is your first day of week.
var d = DateTime.now();
var weekDay = d.weekday;
var firstDayOfWeek = d.subtract(Duration(days: weekDay));
According to Flutter docs weekDay returns
The day of the week [monday]..[sunday].
In accordance with ISO 8601
a week starts with Monday, which has the value 1.
if Monday is first day of week then
var firstDayOfWeek = d.subtract(Duration(days: weekDay - 1));
For Sunday:
DateTime today = DateTime.now();
DateTime _firstDayOfTheweek =
today.subtract(new Duration(days: today.weekday));
print(_firstDayOfTheweek.day);
For Monday:
DateTime _firstDayOfTheweek =
today.subtract(new Duration(days: today.weekday - 1));
print(_firstDayOfTheweek.day);
For Saturday:
DateTime _firstDayOfTheweek =
today.subtract(new Duration(days: today.weekday + 1));
print(_firstDayOfTheweek.day);
The DateTime class stores int constants for the day of the week which you could use to calculate the start date you need. Something like this should work:
void main() {
var startOfWeek = DateTime.saturday;
var currentDate = DateTime.now();
var daysSince = currentDate.weekday + (DateTime.sunday - startOfWeek);
var result = currentDate.subtract(Duration(days: daysSince));
print('Date at start of week is $result');
}
To get different dates of different days in a week. We can use this isoweek package for this. We can use like this :-
Week currentWeek = Week.current();
Week weekFromIso = Week.fromISOString(currentWeek.toString());
print('Week from ISO string: $weekFromIso');
DateTime firstDay= weekFromIso.day(0);
String formattedDateFirst = DateFormat('EEE, d MMM').format(firstDay);
DateTime secondDay= weekFromIso.day(1);
String formattedDateSecond = DateFormat('EEE, d MMM').format(secondDay);

How to get last 30 minutes,1 Hour time from the from current time in DART

how can I get the last 3 hours time from the current time in DART
I am getting the current time by this
DateTime toTime = new DateTime.now();
I want to get last last 30minutes before,lastOneHour,lastThreeHour,last 6,last 12,last 24 time from the current time.
I tried doing like this
from = toTime.subtract(new Duration(hours: 0.5)).millisecondsSinceEpoch;
//This is not working
I am converting those result to epochtime.
As #julemand101 pointed out, Duration has its named hours parameter as an int. Passing a double will toss an error. Instead, write your code something like this:
DateTime curTime = new DateTime.now(); //Current local time
DateTime thirtyMinAgo = curTime.subtract(Duration(minutes: 30));
int thirtyMinAgoInMs = thirtyMinAgo.millisecondsSinceEpoch;
return thirtyMinAgoInMs;
Try out this package, Jiffy. Inspired by momentjs. It also takes into account the leap years and how many days there are in each month
To get the last 30 minutes
Jiffy().subtract(minutes: 30);
// You can also add your DateTime object
Jiffy(DateTime.now()).subtract(minutes: 30);
To get the last 3 hours
Jiffy().subtract(hours: 3);
You can also get up to months and years
var jiffy = Jiffy()
.subtract(minutes: 30, hours: 3, days: 6, months: 2, years: 1);
print(jiffy.dateTime);
// Or you can format it on the go
print(jiffy.format("dd, MMM yyyy"));
print(jiffy.yMMMd);
Hope this helped.

moment.js Timezone Parsing and Comparison

I'm receiving timestamps in the following format '2016-08-17T14:00:00-04:00', which I can parse in moment with moment('2016-08-17T14:00:00-04:00', 'YYYY-MM-DDTHH:mm:ssZ').
But the problem is that I want to print out .format('LLLL') and have it read Wednesday, August 17, 2016 10:00 AM, i.e. subtracting -04:00 from 14:00:00 (NY from UTC). It appears that there is a _tzm: -240 property in the moment object that looks like it holds that -4 hours value, but how do I use that property?
The other goal is to be able to pass in the current time and test if it is between the startDate and endDate variables below. I am guessing if I can convert both to NY-EST I can do this, but I can't seem to get moment to accept the timezone parameter.
Any thoughts?
var moment = require('moment');
// Timestamp strings from API
var startDate = '2016-08-17T14:00:00-04:00';
var endDate = '2016-08-17T15:00:00-04:00';
// Create a range from the start and end dates
var range = moment().range(new Date(startDate), new Date(endDate));
// Get the current time
var currentTime = new Date();
// Does the current time fall within the range b/w the start and end dates
range.contains(currentTime);
A solution I found is below. Adding the value from momentObj._tzm to the parsed date.
module.exports.convertDateToProperTimezone = function (dt) {
var _m = moment(dt, 'YYYY-MM-DDTHH:mm:ssZ');
return _m.add(_m._tzm, 'minutes');
};

converting datetime format to time with only hour and minute

I have values in datetime format. ex: 2012-10-17 00:05:00.000
I casted them into time format ex: 00:05:00.000 but it is still too long for me. I just want hour and minute part of that time. Does anyone know?
Try this:
var dateTime = new DateTime(2012, 10, 17, 0, 5, 0, 0);
var hoursAndMinutes = dateTime.ToShortTimeString();

retrievind date in asp.net

I need to retrieve the current date in asp.net and then compare that with the date given by the user in textbox1.text(mm/dd/yyyy format), if date date given is greater than current date then error else add 4months2days with that date and display it in textbox2.text.
help me please,
thanking you guys,
Indranil
DateTime dateToCompare;
if(DateTime.TryParse(textbox1.text, out dateToCompare))
{
DateTime current = DateTime.Now;
TimeSpan ts = current - dateToCompare;
if (ts.Ticks < 0)
{
//display error
}
else
textbox2.text = dateToCompare.AddMonths(4).AddDays(2).ToString("mm/dd/yyyy");
}
}
I'm not going to write your code, but in .NET you can use ToString to specify a date format, TryParse to get a date out of a string. And AddDays, AddMonths etc to manipulate a date.
In javascript, there's no simple way to format output, but you can use getMonth etc to prompt the individual values and concatenate a string from that. You can use a combination of getDate and setDate to manipulate dates. It automatically corrects for new months, i.e. if you run myDate.setDate( myDate.getDate() + 60 ) it'll actually increment by 60 days; you won't end up with a weird date like May 74th.
Keep in mind that months in javascript are zero-based, ie January is 0, February is 1, etc.
You can create a new date in javascript by new Date(yy, mm, dd) or new Date('yy/mm/dd'), so you could string-manipulate an input and create a date from that.
To compare two dates, you can subtract one from the other, and get the difference in milliseconds.
if ( dateA - dateB < 0 ) // dateB is greater than dateA (occurrs later)
and
var diff = Math.abs(dateA - dateB) // difference in ms, no matter which date is greater
DateTime date1 = new DateTime();
if(DateTime.TryParse(textbox1.text, out date1)){
if (date1.CompareTo(DateTime.Now) > 0)
{
//Error code here
}else
{
textbox2.text = date1.AddMonths(4).AddDays(2);
}
}

Resources