Get time in milliseconds windows

Join the Stack Overflow Community

Stack Overflow is a community of 6.9 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up

As Neil pointed out there is no native solution in cmd. For anyone who has the option of using PowerShell instead, you could use the following:

(Get-Date -UFormat "%Y-%m-%d %H:%M:%S").toString() + "." + ((Get-Date).millisecond)

There may be a more succinct way of doing it but this worked for my purposes.

Not the answer you’re looking for? Browse other questions tagged windows cmd timestamp milliseconds or ask your own question.

Get time in milliseconds on Windows and Linux

Please note that in this case the time expressed in milliseconds is not a conversion of the system time but is a number of milliseconds elapsed by a specific time in the past different according to operating system used. This value is useful if you want to calculate a timeout based to millisecods unit.

 In Microsoft Windows you can call directly this API:

GetTickCount();

This call, based to MSDN documentation, return the number of milliseconds that have elapsed since the system was started, up to 49.7 days.

In Linux you can use this ready-made function:

#include <sys/time.h>

unsigned long GetMillisecondsTime()
{
   struct timeval tv;       
   if(gettimeofday(&tv, NULL) != 0) return 0;
   return (unsigned long)((tv.tv_sec * 1000ul) + (tv.tv_usec / 1000ul));
}

This function return the number of milliseconds elapsed since Epoch.

Popular posts from this blog

Access GPIO from Linux user space

GPIO mean «General Purpose Input/Output» and is a special pin present in some chip that can be set as input or output and used to move a signal high or low (in output mode) or to get the signal current status (in input mode). Usually these pin are directly managed by kernel modules but there are an easy way to manage these pins also from user space.

Android: adb push and read-only file system error

If you want to copy a file from your PC to the connected android device the fasten way is to use adb tool with push command. This command get as params the path of the local file and the remote path to copy in the device. Sometimes, especially if you use adb from Windows or MacOS, and error about read-only file system show up and you can not copy any file.

QTableWidget: center a checkbox inside a cell

QTableWidget is a very good control for show and manage data in a table format. It allow to insert inside cells different type of controls like listbox, checkbox and so on. In this post we’ll discuss about the use of a checkbox control inside a cell with no text and the centering problem.

The solution below seems OK to me. What do you think?

#include <stdio.h>
#include <time.h>

long timediff(clock_t t1, clock_t t2) {
    long elapsed;
    elapsed = ((double)t2 - t1) / CLOCKS_PER_SEC * 1000;
    return elapsed;
}

int main(void) {
    clock_t t1, t2;
    int i;
    float x = 2.7182;
    long elapsed;

    t1 = clock();
    for (i=0; i < 1000000; i++) {
           x = x * 3.1415; 
    }
    t2 = clock();

    elapsed = timediff(t1, t2);
    printf("elapsed: %ld ms\n", elapsed);


    return 0;
}

Reference: http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.15.html#clock

For Windows, GetSystemTime() is what you want. For POSIX, gettimeofday().

A cross platform way is to use ftime.

Windows specific link here: http://msdn.microsoft.com/en-us/library/aa297926(v=vs.60).aspx

Example below.

#include <stdio.h>
#include <sys\timeb.h> 

int main()     
{ 
    struct timeb start, end;
    int diff;
    int i = 0;
    ftime(&start);

    while(i++ < 999) {
        /* do something which takes some time */
        printf(".");    
    }

    ftime(&end);
    diff = (int) (1000.0 * (end.time - start.time)
        + (end.millitm - start.millitm));

    printf("\nOperation took %u milliseconds\n", diff);
    return 0;
}

I ran the code above and traced through it using VS2008 and saw it actually calls the windows GetSystemTimeAsFileTime function.

Anyway, ftime will give you milliseconds precision.

Tags:

Time

C

Elapsed

Related

This code uses the GetLocalTime Windows API to get a more accurate time than VBA will supply, accurate to around 15 milliseconds. You can also do this using a combination of the VBA Date, Time and Timer functions but doing so runs the risk that the calls to the functions will be out of sync … you have to call one before the other(s) and the time will have moved on in-between — you run the risk of, for example, getting a date for ‘today’ using Date then by the time you have called Time to get the time, the actual time may have gone past midnight meaning you get the time for ‘tomorrow’. Unlikely but possible.

As it stands, the code below returns the date and time in a UK format (dd/mm/yy) … if you want another format, just adjust the order that the elements are added in the ConvertSystemTimeToString procedure e.g. for a US format (mm/dd/yy), swap tSystem.wDay and tSystem.wMonth.

In the declarations section of your module, add the following declarations:

And then add the following functions in the main body of the module:

Example usage:

Which prints, to the Immediate window:

Limitations: Windows only

October 5th, 2000, 12:16 PM


#1

Getting Time in MilliSeconds

Hi

How do I get the Total number of MilliSeconds elapsed since Jan 1, 1970 on Windows NT. I am using VC 6.0.

I would appreciate any help or code snippets to do this.

Thanks
Kishore


October 5th, 2000, 12:29 PM


#2

Re: Getting Time in MilliSeconds

CTime pastime(1970, 1, 1, 0,0,0,-1);
CTime currentTime = CTime::GetCurrentTime();
CTimeSpan elapsedTime = currentTime — pastime;
int numDaysPassed = elapsedTime.GetDays();
You can use other member functuons of CTimeSpan class to extract other time field, and to get the elapsed in the way you like.

~ Rating helps serve better, no doubt
~ ks


October 5th, 2000, 12:30 PM


#3

Re: Getting Time in MilliSeconds

You should the function _ftime
Hope it helps
Olivier


October 5th, 2000, 12:30 PM


#4

Re: Getting Time in MilliSeconds


// Microsoft Copyright
#include &lt;time.h&gt;
#include &lt;stdio.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/timeb.h&gt;
#include &lt;string.h&gt;

void main()
{
char tmpbuf[128], ampm[] = «AM»;
time_t ltime;
struct _timeb tstruct;
struct tm *today, *gmt, xmas = { 0, 0, 12, 25, 11, 93 };

/* Set time zone from TZ environment variable. If TZ is not set,
* the operating system is queried to obtain the default value
* for the variable.
*/
_tzset();

/* Display operating system-style date and time. */
_strtime( tmpbuf );
printf( «OS time:\t\t\t\t%s\n», tmpbuf );
_strdate( tmpbuf );
printf( «OS date:\t\t\t\t%s\n», tmpbuf );

/* Get UNIX-style time and display as number and string. */
time( &ltime );
printf( «Time in seconds since UTC 1/1/70:\t%ld\n», ltime );
printf( «UNIX time and date:\t\t\t%s», ctime( &ltime ) );

/* Display UTC. */
gmt = gmtime( &ltime );
printf( «Coordinated universal time:\t\t%s», asctime( gmt ) );

/* Convert to time structure and adjust for PM if necessary. */
today = localtime( &ltime );
if( today-&gt;tm_hour &gt; 12 )
{
strcpy( ampm, «PM» );
today-&gt;tm_hour -= 12;
}
if( today-&gt;tm_hour == 0 ) /* Adjust if midnight hour. */
today-&gt;tm_hour = 12;

/* Note how pointer addition is used to skip the first 11
* characters and printf is used to trim off terminating
* characters.
*/
printf( «12-hour time:\t\t\t\t%.8s %s\n»,
asctime( today ) + 11, ampm );

/* Print additional time information. */
_ftime( &tstruct );
printf( «Plus milliseconds:\t\t\t%u\n», tstruct.millitm );
printf( «Zone difference in seconds from UTC:\t%u\n»,
tstruct.timezone );
printf( «Time zone name:\t\t\t\t%s\n», _tzname[0] );
printf( «Daylight savings:\t\t\t%s\n»,
tstruct.dstflag ? «YES» : «NO» );

/* Make time for noon on Christmas, 1993. */
if( mktime( &xmas ) != (time_t)-1 )
printf( «Christmas\t\t\t\t%s\n», asctime( &xmas ) );

/* Use time structure to build a customized time string. */
today = localtime( &ltime );

/* Use strftime to build a customized time string. */
strftime( tmpbuf, 128,
«Today is %A, day %d of %B in the year %Y.\n», today );
printf( tmpbuf );
}


October 5th, 2000, 12:37 PM


#5

Re: Getting Time in MilliSeconds

I do not know of any one-step way to do this, but you could do that:
1. Get current time (to milliseconts)
SYSTEMTIME systime;
GetSystemTime(&systime);

2. Construct two CTime objects, one from systime, and one from your date (Jan 1, 1970).
3. Calculate CTimeSpan between them two.
4. Get CTimeSpan::GetTotalSeconds().
5. Multiply by 1,000 and add milliseconds from you original systime.
6. Watch for int overflow!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I do not do it for the rating. Oh, wait… Sure, I do!

Vlad — MS MVP [2007 — 2012] — www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows — replacement windows manager for Visual Studio, and more…


Понравилась статья? Поделить с друзьями:
0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии
  • Activate windows 10 using cmd
  • Перестал работать микрофон windows 10 после обновления
  • Update windows 7 home premium
  • Как менять окна в windows 10
  • Бесплатное меню пуск для windows 10