C# Date Conversion To UnixTimeCode

PHaRTnONu

Client
Регистрация
01.10.2016
Сообщения
340
Благодарностей
48
Баллы
28
BEFORE YOU EXECUTE THIS
You need to add the line
using System.Globalization;
To your Directives & Shared code.
Im including a screen shot so you can see, to access it again you'll notice in the right-hand side a new icon at the bottom of the photo. That is how you access it again if you need to.
In this code example im taking the date in the zennoposter var DateToProcess and tossing back the unix time code.
100389


Date To Unix Conversion:
string[] formats = {"MMMM dd, yyyy","dd MMMM, yyyy","dd MMMM yyyy","dd MMMM yy","dd MMM yyyy","dd MMM yy","dd-MMM-yyyy","dd-MMM-yy","dd/MMM/yyyy","dd/MMM/yy","yyyy-MM-dd","yyyy-dd-MM","yy-MM-dd","yy-dd-MM","yyyy/MM/dd","yyyy/dd/MM","yy/MM/dd","yy/dd/MM","MMM dd yyyy","MMM dd yy","MMM-dd-yyyy","MMM-dd-yy","MMM/dd/yyyy","MMM/dd/yy","MM dd yyyy","MM dd yy","MM-dd-yyyy","MM-dd-yy","MM/dd/yyyy","MM/dd/yy","dd MM yyyy","dd MM yy","dd-MM-yyyy","dd-MM-yy","dd/MM/yyyy","dd/MM/yy","yyyy MM dd","yyyy dd MM","yy MM dd","yy dd MM","yyyy/MM/dd","yyyy/dd/MM","yy/MM/dd","yy/dd/MM"}; 

// Try to parse the date string using each of the specified formats.
// If the parse is successful, convert the date to a Unix timestamp and return it.
foreach (string format in formats)
{
    DateTime datestr;
    if (DateTime.TryParseExact(project.Variables["DateToProcess"].Value, format, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out datestr))
    {
        DateTime date = Convert.ToDateTime(datestr);
        int unix = (Int32)(date.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
        return unix;
    }
}

// If the date string could not be parsed using any of the specified formats,
// throw an exception indicating that the date format is invalid.
throw new FormatException("Invalid date format");
 
  • Спасибо
Реакции: Sho и myndeswx

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)