How to make time ago from json in flutter - flutter

Is it possible if I have json with the timestamp like this
{
"modified": "2018-11-14T16:25:21",
}
and I want to make time ago in flutter. eg.5 minutes ago

import 'package:simple_moment/simple_moment.dart';
main() {
var datetoCompare = new Duration(seconds: 21,minute : 25, hour:16); //And so with month, year and Day.
var dateForComparison = new DateTime.now().add(datetoCompare);
var moment = new Moment.now();
print(moment.from(dateForComparison));
}
This Package helps in showing relative time in flutter.
Simple Moment package -
https://pub.dev/packages/simple_moment#-readme-tab-

For Seconds:
yourDate.difference(DateTime.now()).inSeconds;
For Minutes:
yourDate.difference(DateTime.now()).inMinutes;
For Hours:
yourDate.difference(DateTime.now()).inHours;

Related

Is it possible to have a time interval in Flutter?

I wondering if there is any way to get a time interval for an item in Flutter?
Example:
I have an item where I set a date when this item was last calibrated, and then I want the application to remind me (with either a push message or an email) after a set period of time for the next calibration and so on.
I use Firestore for my database.
You may want to take a look to the Duration Dart's class.
Docs: https://api.dart.dev/stable/2.18.4/dart-core/Duration-class.html
To give you an idea, fast code example to initialize a span of time:
const fastestMarathon = Duration(hours: 2, minutes: 3, seconds: 2);
print(fastestMarathon.inDays); // 0
print(fastestMarathon.inHours); // 2
print(fastestMarathon.inMinutes); // 123
print(fastestMarathon.inSeconds); // 7382
print(fastestMarathon.inMilliseconds); // 7382000
You can look at the timeago package implementation to compare times.
dependencies:
timeago: ^3.3.0
You can use time ago for the exact purpose and it is quite beneficial. It has multiple formats and different language support as well.
import 'package:timeago/timeago.dart' as timeago;
main() {
final fifteenAgo = DateTime.now().subtract(Duration(minutes: 15));
print(timeago.format(fifteenAgo)); // 15 minutes ago
print(timeago.format(fifteenAgo, locale: 'en_short')); // 15m
print(timeago.format(fifteenAgo, locale: 'es')); // hace 15 minutos
}

How do i get the current date and time of places around the world in flutter

i am currently working on a world clock app using flutter, can any one recommend dependencies or API i can integrate into my app that provide a list of places around the world, another issue i am having is getting the app to display current date and time of those selected places
The DateTime class of Dart supports UTC and the local time zone. If you want to calculate a timestamp to a different time zone, check out the timezone package on pub.dev.
https://pub.dev/packages/timezone
It supports something like this for example:
import 'package:timezone/standalone.dart' as tz;
Future<void> setup() async {
await tz.initializeTimeZone();
var detroit = tz.getLocation('America/Detroit');
var now = tz.TZDateTime.now(detroit);
}
It also consists of a time zone database:
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;
void main() {
tz.initializeTimeZones();
var locations = tz.timeZoneDatabase.locations;
print(locations.length); // => 429
print(locations.keys.first); // => "Africa/Abidjan"
print(locations.keys.last); // => "US/Pacific"
}

Flutter DropdownMenuItem delete

I'm developing a combobox using Flutter. combobox needs to be this month and next months. should write month name in combobox and return the number of the selected month. How can I do this in the shortest way?
I developed 12 months as DropdownMenuItem. I gave the current month as the default value. So far everything is fine. but how can I make the items of the past months invisible?
I used this code for a similar problem.
This solution includes intl package but you can change the code in list generation for your purpose.
import 'package:intl/intl.dart';
void main(){
var today = DateTime.now();
var length = 12 - today.month + 1; // I added +1 because I want to include current month too
List validMonths = List.generate(length,(index){
return DateFormat('MMMM').format(DateTime(0, (today.month + index)));
});
print(validMonths);
}

Ionic 3/4 - Daily Local Notifications not working

Local notifications don't work properly (tried it with ionic 3 & 4).
The user can set a time in the app and turn the notifications on or off.
When using the following code, I always get a notification at 01:00 am although I've set it to 17:30 or something else.
I tried many variations, this is the last one:
const time = setsub.task_reminder_time.split(':');
const now = new Date();
console.log('now is', now);
const pushDate = new Date(now.getFullYear(), now.getMonth(), now.getDate(), +time[0], +time[1], 0);
const options = {
id: 1,
title: 'Time to plan your day!',
text: `Hey you! It's time to get productive by planning your day!\nTap here to open DoDay! 🤓`,
trigger: { firstAt: pushDate, every: ELocalNotificationTriggerUnit.DAY }
};
if (setsub.task_reminder) {
this.notification.requestPermission();
this.notification.schedule(options);
} else {
this.notification.clearAll();
}
time is just a string containing the notification time in HH:mm
I'm using an iOS device for testing.
So I found the solution to this one by myself. It's an error in the typings file of LocalNotifications package.
The correct usage for the options look like this:
{
id: 1,
title: 'Notification Title',
text: 'Your notification text',
foreground: true,
trigger: {
every: {
hour: 8,
minute: 15
}
}
}
Just go into your node_modules/#ionic-native/local-notifications find the index.d.ts and find the line which says every?: ELocalNotificationTriggerUnit and change it to every?: any; now it should work perfectly.

JQuery UI Datepicker Current Day

My page uses a JQuery UI Datepicker and loads with the current day selected and weekends and selected dates restricted.
I would like the current selected day to become unavailable at 2pm and the day move forward.
Can anyone help.
I would just use a variable for the the minDate
var dt = new Date();
if (dt.getHours() > 14) {
dt = dt.setDate(dt + 1); // go one day in the future
}
$(selector).datepicker({minDate: dt});
You'd just need to use the real javascript Date class methods - which I haven't used in a little while.