First, get the current time and switch to a specific format:
String tradetime = DateTime.Now.ToString ("Yyyymmddhhmmss", datetimeformatinfo.invariantinfo);
Set the time format you want:
String tradetime = DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss", datetimeformatinfo.invariantinfo);
String tradetime = DateTime.Now.ToString ("yyyy mm month DD Day hh when mm minute ss seconds", Datetimeformatinfo.invariantinfo);
Second, get the current timestamp (the time to get the timestamp is 13 digits, only seconds to the time stamp is 10 bits)
The first method
///<summary>
///Gets the current timestamp
///</summary>
///<param name= "Bflag" > Gets a 10-bit timestamp when true, and gets a 13-bit timestamp when false. bool Bflag = true</param>
///<returns></returns> public
static String Gettimestamp (bool bflag)
{
TimeSpan ts = datetime.utcnow-new DateTime (1970, 1, 1, 0, 0, 0, 0);
string ret = string. Empty;
if (bflag)
ret = Convert.toint64 (ts. totalseconds). ToString ();
else
ret = convert.toint64 (ts. TotalMilliseconds). ToString ();
return ret;
}
In general one project will be only one, or 10-bit or 13-bit, so you can also directly use the following code
public static long Gettimestamp ()
{
TimeSpan ts = DateTime.Now.ToUniversalTime ()-New DateTime (1970, 1, 1);//tou Niversaltime () to the time of the standard time zone, remove the words directly with the Beijing Time return
(long) ts. TotalMilliseconds; Accurate to millisecond
//return (long) ts. totalseconds;//get 10-bit
}
The only difference in the following way is whether you need to get an exception.
Generate UNIX format time public
static Long Getunix ()
{
try
{
TimeSpan TimeSpan = datetime.utcnow-new DateTime (1970, 1, 1);
Return (long) TimeSpan. totalseconds;//gets the 10-bit return
(long) TimeSpan. totalmilliseconds;
}
catch (Exception)
{
return-1
}
}
The first four complete methods and the results of operation
public static long Gettimestamp () {TimeSpan ts = DateTime.Now.ToUniversalTime ()-New DateTime (1970, 1, 1),//touniversaltime () to the time of the standard time zone, remove the words directly with the Beijing Time return (long) TS. TotalMilliseconds; Accurate to millisecond//return (long) ts.
totalseconds;//get 10-bit}//generate UNIX format time public static long Getunix () {try
{TimeSpan TimeSpan = datetime.utcnow-new DateTime (1970, 1, 1); Return (long) TimeSpan. totalseconds;//gets the 10-bit return (long) TimeSpan.
TotalMilliseconds;
catch (Exception) {return-1; The public static long Datetimetounixtimestamp () {var start = new DateTime (1970, 1,
1, 0, 0, 0, DateTime.Now.Kind); Return Convert.toint64 ((Datetime.now-start).
TotalSeconds); public static long Getcurrenttimeunix () {TimeSpan chA = (Datetime.now-timezone.currenttimezone.tolocaltime (New System.DateTime (1970, 1, 1)); Long T = (long) cha.
TotalSeconds;
return t; }
Run Results:
When you see the results, you'll find seconds. That's a little different, probably the difference between Beijing time and standard Time zone, where there is a mistake, please leave a message.