I need to add 24 hr on the selected date slot in the calendar in milliseconds since the epoch.
As in the below code if I add 24 hr it will give me 00:00 time for the same day so I tried to use this (23, minutes: 59, seconds: 59) which is wrong.
Please suggest...
DateTime initialTime = selectedDate.value;
DateTime finalTime = selectedDate.value.add(new Duration(hours: 23,minutes: 59,seconds: 59));
int finalTimeInEpoch = finalTime.millisecondsSinceEpoch;
print('This is the initial time for selected day --> $initialTime and This is the final time for selected day --> $finalTime');
int initialTimeForApi = initialTime.millisecondsSinceEpoch;
int finalTimeForApi= finalTimeInEpoch;
print('This is the initial time for selected day --> ${initialTime.millisecondsSinceEpoch} and This is the final time for selected day --> $finalTimeInEpoch in millisecondsSinceEpoch');
Use Like This
DateTime finalTime = selectedDate.value.add(Duration(hours: 24));
if this don fix the issue your problem is selectedDate.value is not a valid format date time
Related
I want to implement week interval feature where I can go on next by clicking on forward button.
First of all you can calculate the first and last day of the week by using this functions:
/// Find the first date of the week given a date.
/// ```
/// findFirstDateOfTheWeek(DateTime.now());
///```
DateTime findFirstDateOfTheWeek(DateTime dateTime) {
return dateTime.subtract(Duration(days: dateTime.weekday - 1));
}
/// Find last date of the week given a date.
///```
/// findLastDateOfTheWeek(DateTime.now());
///```
DateTime findLastDateOfTheWeek(DateTime dateTime) {
return dateTime.add(Duration(days: DateTime.daysPerWeek - dateTime.weekday));
}
Example:
DateTime now = DateTime.now();
DateTime fday = findFirstDateOfTheWeek(now );
DateTime lday = findLastDateOfTheWeek(now);
Once you got the first two values you can add 7 days to the date time passed to get the following week and so on:
DateTime.now().add(Duration(days: 7));
Hope it helps at least to get you started.
I am taking startdate and endDate as input from user with DateTime as datatype in Flutter(Dart). These fields will be stored in Firestore may be as timestamp format.
Now we need to display the difference of endDate and startDate on client side which can be a live timer in format as "13 Hrs 45 mins" then after some mins it should be "13 Hrs 42 mins".
May I know how can we achieve this using Firestore or may be cloud functions for Flutter apps?
Thanks
In flutter, Timestamp can be easily converted to DateTime, and DateTime has convenient method called difference that will give you difference between two DateTime objects as Duration object.
Here is a simple example:
final Timestamp endTime = Timestamp(1000, 0);
final Timestamp startTime = Timestamp(500, 0);
final DateTime endTimeDate = endTime.toDate();
final DateTime startTimeDate = startTime.toDate();
final Duration difference = endTimeDate.difference(startTimeDate);
can i set datetime.now() in GMT timezone or can i set datetime.now() -7 hours ???
this is the variable
String formattedDate = DateFormat('yyyy-MM-dd HH:mm:ss').format(DateTime.now());
im clueless
You can add or substract from a date time by using Duration.
Date dateA = someDate.subtract(Duration( hours: 6 ));
I am new to lua scripting. I have a startDate ("03-05-2014" as "dd-mm-yyyy") and a span in days (2) Can anyone help me how to get the endDate based on the startDate and span?.
Example startDate span endDate
--------- ---- -------
03-05-2014 2 05-05-2014
(dd-mm-yyyy) (dd-mm-2014)
You don't need to do any math here. os.time and os.date will do it for you.
local day, month, year = ("03-05-2014"):match("(%d%d)-(%d%d)-(%d%d%d%d)")
local span = 64
local endtime = os.time({day = day + span, month = month, year = year})
print(os.date("%c", endtime))
I'm not going to write the whole program for you, but here's something you can start with:
Get the day, month and year from the string:
local day, month, year = string.match('03-05-2014', '(%d%d)-(%d%d)-(%d%d%d%d)')
day, month, year = tonumber(day), tonumber(month), tonumber(year)
Use os.time to get the timestamp of a start time. You can
then add 3600 * 24 * 2 seconds (2 days) to get the timestamp of the end time.
Use os.date to formats the string from a timestamp.
This could help you
local dayValue, monthValue, yearValue = string.match('31-05-2014', '(%d%d)-(%d%d)-(%d%d%d%d)')
dayValue, monthValue, yearValue = tonumber(dayValue), tonumber(monthValue), tonumber(yearValue)
now = os.time{year = yearValue, month = monthValue, day = dayValue}
numberOfDays = now + 2 * 24 * 3600
print(os.date("%c",numberOfDays))
dateAfterNumberOfDays = os.date("%a %d %B %Y, %H%p%M",numberOfDays)
print ("\nafter number of days "..dateAfterNumberOfDays) -- give you date after number of days
I have this problem. I need to do the following:
get todays date
make a new date which will be today's date at 00:00:00
make another date which will be today's date at 23:59:59
For example. Today Date is 12-January-2012 19:00
How can i make a new date, which will be 12-January-2012 00:00 (the start of the current day)
It may seems easy, but i couldnt find any groovyway to get it, any help would be apreciated.
To get the date at midnight use Date.clearTime (docs):
dateAtMidnight = new Date()
dateAtMidnight.clearTime()
(Javadocs are for Groovy JDK < 2.0, clearTime() is declared void in Groovy JDK 2.0, preventing d = new Date().clearTime(). Comments indicate the original functionality may be restored, yay!)
For the comparison, instead of using <= 23:59:59, use < (the next day):
(aDate >= dateAtMidnight) && (aDate < (dateAtMidnight + 1))
An alternative way, but it sets the datetime (but it doesn't get the date merely)
dateAtMidnight = new Date()
dateAtMidnight.set(hourOfDay: 0, minute: 0, second: 0)