How to print time in format: 2009‐08‐10 18:17:54.811

What's the best method to print out time in C in the format 2009‐08‐10 

-------------Problems Reply------------

Use strftime().

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

int main()
time_t timer;
char buffer[26];
struct tm* tm_info;

tm_info = localtime(&timer);

strftime(buffer, 26, "%Y:%m:%d %H:%M:%S", tm_info);

return 0;

For milliseconds part, have a look at this question. How to measure time in milliseconds using ANSI C?

time.h defines a strftime function which can give you a textual representation of a time_t using something like:

#include <stdio.h>
#include <time.h>
int main (void) {
char buff[100];
time_t now = time (0);
strftime (buff, 100, "%Y-%m-%d %H:%M:%S.000", localtime (&now));
printf ("%s\n", buff);
return 0;

but that won't give you sub-second resolution since that's not available from a time_t. It outputs:

2010-09-09 10:08:34.000

If you're really constrained by the specs and do not want the space between the day and hour, just remove it from the format string.

You could use strftime, but struct tm doesn't have resolution for parts of seconds. I'm not sure if that's absolutely required for your purposes.

struct tm tm;
/* Set tm to the correct time */
char s[20]; /* strlen("2009-08-10 18:17:54") + 1 */
strftime(s, 20, "%F %H:%M:%S", &tm);

Category:c# Views:0 Time:2010-09-09
Tags: c# time

Related post

  • Oracle Date formatting "2009-02-13T11:46:40+00:00" 2009-05-05

    I've had some brilliant help before and I'm hoping you can get me out of a hole again. I've got a date coming in from a web service in this format: 2009-02-13T11:46:40+00:00 which to me looks like standard UTC format. I need to insert it into an Orac

  • how to print from POS printer in predefined format 2010-11-08

    I want to print bill through POS printer. It should print in following format which i am displaying to salesman in FlowDocument control. Item CODE Qty Price Total ------------------------------------------------ ABcDEFG 10 $5 $50 XYZ 5 $10 $50 ------

  • efficient way to print out my format 2011-11-08

    I wish there is an efficient way to print out my format. As I know convert to string may occur performance issue. Is there any better method? package main import "fmt" type T struct { order_no [5]byte qty int32 } func (t T)String() string { return fm

  • I just installed windows 7. I want to be able to scan documents from hp4315v printer in PDF format to send an E-mail in PDF format 2012-04-27

    Original title: Windows 7 I just installwd windows 7 I want to be able to scan documents from hp4315v printer in PDF format to send an E-mail in PDF format --------------Solutions------------- Hi, 1. Have you installed the latest Adobe reader for Win

  • When I click finish and print, the list format prints instead of labels. 2013-02-27

    Mail merge I have completed many label mail merges in the past, but I am having a problem when I get to the preview step. When I click on preview, the format changes from the address labels to a list and when I click finish and print, the list format

  • php fetch file content and print it out formatted 2015-02-11

    How can i print out an formatted HTML file ? This is my current code: echo htmlspecialchars(file_get_contents($url)); But the result looks so ugly, all sticks together, no space no tabs. Is there any way to print the file exactly like the original fi

  • Is there a printf converter to print in binary format? 2008-09-21

    I can print with printf as a hex or octal number. Is there a format tag to print as binary, or arbitrary base? I am running gcc. printf("%d %x %o\n", 10, 10, 10); //prints "10 A 12\n" print("%b\n", 10); // prints "%b\n" --------------Solutions-------

  • PHP Print Date Variable Format 2009-05-19

    So this should be a real easy question but I can't seem to find a simple answer anywhere. I'm patching up some PHP code (I'm not a PHP'er) and I have this variable $orderDate. How do I print this variable so that its just M/d/yy h:mm tt? Update: So I

  • Printing JTables without formatting of the original component 2010-05-24

    I'm writing an application which utilises tables which can be printed if the user so desires and I wish to print a JTable filled with data, except I haven't been able to find an option to remove the formatting; the printed tables looks like it does i

  • Print informative, well formatted mysql errors in PHP 2010-08-09

    Mysql errors are among the most common ones you see during development, and I am searching for a pleasant and informative way to output them. phpMyAdmin apparently has a decent pretty-printer for mySQL queries: However, it prints errors as simple tex

  • is there a DevExpress XtraReports event that fires after all widgets of a details subreport got printed, like for formatting? 2011-04-13

    I want to be able to go through all instances of a printed details subreport and do some formatting of records, in part by computing stuff for one record based on another one already printed. The code that prints reports is a bit of a mess, so I thin

  • How to set the print statement A5 format? 2011-08-10

    when I pressed the print button. I would like to Print statement in A5 format. Two identical printing statement side by side on one A4 page in landscape in php and using table for forms. is it possible? or any other method are there? --------------So

  • Print out square format java 2011-10-23

    I need to print out a square of hashes in the following type of format: #### #### #### #### I would like to have an input which will change the dimensions of the square based on a user's choice. Any idea what is the best way to create a reusable meth

  • Shell Script Printing in table format 2011-11-24

    I am trying to print set of values in table format using shell script . The table has n rows and 4 columns. I tried the following piece of code. btfield=`grep -i -r ^"diag.* <string>" *.txt |awk '{print $5}'|cut -d+ -f 1 |sort -u` ipaddr='' i=0

  • What does the % in a print between the format and variables actually do? 2012-01-13

    I'm just starting to fool around with formatting the output of a print statement. The examples I've seen have a % after the format list and before the arguments, like this: >>> a=123 >>> print "%d" % a 123 What is the meaning of the

  • Most elegant way to print differing file formats stored as BLOBs 2012-03-30

    I have a table with checkboxes that corresponds to BLOBs kept in MySQL database. These are all different types of files txt, xls, doc, pdf, etc. I am new to programming so I am not sure how to best allow my users to print these files in groups? I und

  • WriteFile use to print in some format 2012-04-24

    Is it possible to use WriteFile in order to print formatted data?(The data which is formatted based on some modifiers/specifiers) I can't use fprintf. The possible solution I thought about is to use sprintf to print to the buffer and then WriteFile t

  • Designing and printing a receipt format in flex 2012-04-26

    I want to design a Receipt in flex. Which control shall I use to display a table if I want to display some table cells without border? I also want to merge cells in the table just as we used to do in Excel. Can anyone please give me the code to print

  • Excel 2003 - printing on large format printer 2013-10-08

    I have a speradsheet that is far too big to print on 8 1/2" x 11" paper, (it would take 25 sheets) but I have a large format printer (HPT1100PS). I have tried to change the paper siz so I can print the entire sheet in one go but there doesn't appera

Copyright (C), All Rights Reserved.

processed in 0.154 (s). 11 q(s)