Rust
Rust
Compute Difference Between Two DateTime Strings
Demonstrates how to compute the difference between two datetime strings.Chilkat Rust Downloads
// Imagine we have these strings:
let t1 = "2022-11-14 15:45:38".to_string();
let t2 = "2022-11-16 17:23:41".to_string();
// How many minutes are between the two?
// First, we'd want load each date/time into a Chilkat CkDateTime object.
// In this case, there are no CkDateTime methods that accept the above format.
// However.. the CkDateTime's SetFromTimestamp will accept a date in the format "YYYY-MM-DDThh:mm:ssZ"
// First modify the above dates to the timestamp format.
let sb1 = chilkat::StringBuilder::new();
let _ = sb1.append(&t1);
let mut count = sb1.replace(" ", "T");
let _ = sb1.append("Z");
let sb2 = chilkat::StringBuilder::new();
let _ = sb2.append(&t2);
count = sb2.replace(" ", "T");
let _ = sb2.append("Z");
// Load each into a CkDateTime
let dt1 = chilkat::DateTime::new();
let _ = dt1.set_from_timestamp(&sb1.get_as_string().unwrap_or_default());
// verify...
println!("{}", dt1.get_as_timestamp(false).unwrap_or_default());
let dt2 = chilkat::DateTime::new();
let _ = dt2.set_from_timestamp(&sb2.get_as_string().unwrap_or_default());
// verify...
println!("{}", dt2.get_as_timestamp(false).unwrap_or_default());
// Get the difference in seconds
let diff_seconds = dt2.diff_seconds(&dt1);
println!("Difference in seconds: {}", diff_seconds);
println!("Difference in minutes: {}", diff_seconds / 60);