'convert time to unix timestamps since epoch and send it tyo server flutter

how can i convert datetime to unix time using datetimepicker package? I need user can choose the date and time and after their choice the chosen date and time convert to unix timestamp and send this timestamp to server.

Now I am trying something like this:

var dateUnix = DateTime.now().toUtc().millisecondsSinceEpoch.toString();
    child: DateTimePicker(
                    type: DateTimePickerType.dateTimeSeparate,
                    dateMask: 'EEE, d MMM yyyy HH:mm:ss',
                    initialValue: dateUnix 
                    firstDate: DateTime(2000),
                    lastDate: DateTime(2100),
                    icon: Icon(Icons.event),
                    dateLabelText: 'Date',
                    timeLabelText: "Hour",
                    onChanged: (val) => print(val),
                    validator: (val) {
                      print(val);
                      return null;
                    },
                    onSaved: (val) => print(val),

the output is still:

I/flutter (11990): 2022-02-07 12:10

I need it aoutomatucally converted to unix-time:

Epoch timestamp: 1644235837


Solution 1:[1]

Use the following in your print statement:

print(val.toUtc().millisecondsSinceEpoch.toString())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BJW