How can I convert a Unix timestamp to DateTime and vice versa?

Here’s what you need: public static DateTime UnixTimeStampToDateTime( double unixTimeStamp ) { // Unix timestamp is seconds past epoch DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); dateTime = dateTime.AddSeconds( unixTimeStamp ).ToLocalTime(); return dateTime; } Or, for Java (which is different because the timestamp is in milliseconds, not seconds): public static … Read more

Given a DateTime object, how do I get an ISO 8601 date in string format?

Note to readers: Several commenters have pointed out some problems in this answer (related particularly to the first suggestion). Refer to the comments section for more information. DateTime.UtcNow.ToString(“yyyy-MM-ddTHH\\:mm\\:ss.fffffffzzz”, CultureInfo.InvariantCulture); Using custom date-time formatting, this gives you a date similar to 2008-09-22T13:57:31.2311892-04:00. Another way is: DateTime.UtcNow.ToString(“o”, CultureInfo.InvariantCulture); which uses the standard “round-trip” style (ISO 8601) to … Read more

DateTime vs DateTimeOffset

DateTimeOffset is a representation of instantaneous time (also known as absolute time). By that, I mean a moment in time that is universal for everyone (not accounting for leap seconds, or the relativistic effects of time dilation). Another way to represent instantaneous time is with a DateTime where .Kind is DateTimeKind.Utc. This is distinct from … Read more

How do I get the current date and time in PHP?

The time would go by your server time. An easy workaround for this is to manually set the timezone by using date_default_timezone_set before the date() or time() functions are called to. I’m in Melbourne, Australia so I have something like this: date_default_timezone_set(‘Australia/Melbourne’); Or another example is LA – US: date_default_timezone_set(‘America/Los_Angeles’); You can also see what … Read more

How to sort an object array by date property?

Simplest Answer array.sort(function(a,b){ // Turn your strings into dates, and then subtract them // to get a value that is either negative, positive, or zero. return new Date(b.date) – new Date(a.date); }); More Generic Answer array.sort(function(o1,o2){ if (sort_o1_before_o2) return -1; else if(sort_o1_after_o2) return 1; else return 0; }); Or more tersely: array.sort(function(o1,o2){ return sort_o1_before_o2 ? … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)