Sponsored Content
Top Forums Shell Programming and Scripting Python script to convert date to iso format Post 302864563 by targzeta on Wednesday 16th of October 2013 05:33:33 PM
Old 10-16-2013
Code:
from time import strptime, strftime

date_in = '10/16/2013'

date_out = strftime('%Y-%m-%d', strptime(date_in, '%m/%d/%Y'))

print date_out

Emanuele
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

convert mmddyy date format to ccyyddd format??

hi, for reading a cobol indexed file i need to convert "mmddyy" date format to "ccyyddd" format. i checked the datecalc and other scripts but couldnt modify them to cater to my need:(... The datecalc gives an output which i believe is the total days till that date, but i want to convert it... (2 Replies)
Discussion started by: Bhups
2 Replies

2. UNIX for Dummies Questions & Answers

Convert date format

Hi All, Need your help in converting a date format in ksh. I'm currently working on SUN os where my script is getting a date from a table. The result returns to ksh in this format: 17-JUL-08 How do i convert this string to a date format like yyyymmdd? I tried #!/bin/ksh d="17-JUL-08"... (5 Replies)
Discussion started by: agathaeleanor
5 Replies

3. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

4. Shell Programming and Scripting

convert date format

I've been using this thread: https://www.unix.com/shell-programming-scripting/58675-change-date-dd-mmm-yyyy-mm-dd-yyyy.html and https://www.unix.com/shell-programming-scripting/14655-changing-yyyy-mm-dd-ddmmyy.html and this code: on this format: 05/16/2008 18:30:49 Installation 48985and I'm... (3 Replies)
Discussion started by: dba_frog
3 Replies

5. Shell Programming and Scripting

Need a unix script to convert date into Julian format in a text file

The 6th & 7th column of the text files represents date & time. I need this to be converted in julian format using command "date +%s -d <date>". I know the command, but dont know how to use it on the script 0 dbclstr-b IXT_Web Memphis_Prod_SQL_Full Memphis-Prod-SQL-Full-Application-Backup... (4 Replies)
Discussion started by: ajiwww
4 Replies

6. Shell Programming and Scripting

Date Format not working in python script

Good Morning, I am working on a pyhton script, where it has to fetch a file from our vendor's server over an ftp. The file is having a date format of "MMDDYYYY" (i have included in my python script), but when the script is executed it is unable to fetch the file. At the same time, if i... (1 Reply)
Discussion started by: AReddy
1 Replies

7. Shell Programming and Scripting

Convert string into date format

Hi, I am getting the below string as a input for date. 12/03/2013 11:02 AM I want to change this date as 03-DEC-2013 11:02 AM. Could you please help on this. Thanks Chelladurai (4 Replies)
Discussion started by: ckchelladurai
4 Replies

8. Shell Programming and Scripting

Convert Date Format

Hello, I want to change the format of date value in variable. e.g. cur_date = '2013/03/13 14:24:50' (yyyy/mm/dd hh24:mi:ss) I want to change this to '13-MAR-2013 14:24:50 Following code coverts the current date to format I am looking for. But I do not know how this can be done for a date... (8 Replies)
Discussion started by: cartrider
8 Replies

9. Shell Programming and Scripting

Convert date format

Hi guys I am looking to convert this kind of entry in a txt file 26/04/2008 to April 2008 Note : this is not using the date command , these are date entries in a file can i do this with sed ? Use code tags, thanks. (5 Replies)
Discussion started by: johnnybananas
5 Replies

10. Shell Programming and Scripting

Convert excel to csv in python date not display exactly

Hi, Anyone can help I am just converting the excel file to csv using python, I can get the conversion output but the date not display exactly. test.xlsx date format 167 1588 risks/SCS JP CAMPANA & CIE.pdf SCS JP CAMPANA & CIE 2 1 1 0 2015-03-16 16:56:25 167 1146 risks/AirBNB... (1 Reply)
Discussion started by: fspalero
1 Replies
STRPTIME(3)						     Linux Programmer's Manual						       STRPTIME(3)

NAME
strptime - convert a string representation of time to a time tm structure SYNOPSIS
#define _XOPEN_SOURCE /* glibc2 needs this */ #include <time.h> char *strptime(const char *s, const char *format, struct tm *tm); DESCRIPTION
The strptime() function is the converse function to strftime() and converts the character string pointed to by s to values which are stored in the tm structure pointed to by tm, using the format specified by format. Here format is a character string that consists of field descriptors and text characters, reminiscent of scanf(3). Each field descriptor consists of a % character followed by another character that specifies the replacement for the field descriptor. All other characters in the format string must have a matching character in the input string, except for whitespace, which matches zero or more whitespace characters in the input string. There should be whitespace or other alphanumeric characters between any two field descriptors. The strptime() function processes the input string from left to right. Each of the three possible input elements (whitespace, literal, or format) are handled one after the other. If the input cannot be matched to the format string the function stops. The remainder of the format and input strings are not processed. The supported input field descriptors are listed below. In case a text string (such as a weekday or month name) is to be matched, the com- parison is case insensitive. In case a number is to be matched, leading zeros are permitted but not required. %% The % character. %a or %A The weekday name according to the current locale, in abbreviated form or the full name. %b or %B or %h The month name according to the current locale, in abbreviated form or the full name. %c The date and time representation for the current locale. %C The century number (0-99). %d or %e The day of month (1-31). %D Equivalent to %m/%d/%y. (This is the American style date, very confusing to non-Americans, especially since %d/%m/%y is widely used in Europe. The ISO 8601 standard format is %Y-%m-%d.) %H The hour (0-23). %I The hour on a 12-hour clock (1-12). %j The day number in the year (1-366). %m The month number (1-12). %M The minute (0-59). %n Arbitrary whitespace. %p The locale's equivalent of AM or PM. (Note: there may be none.) %r The 12-hour clock time (using the locale's AM or PM). In the POSIX locale equivalent to %I:%M:%S %p. If t_fmt_ampm is empty in the LC_TIME part of the current locale then the behaviour is undefined. %R Equivalent to %H:%M. %S The second (0-60; 60 may occur for leap seconds; earlier also 61 was allowed). %t Arbitrary whitespace. %T Equivalent to %H:%M:%S. %U The week number with Sunday the first day of the week (0-53). The first Sunday of January is the first day of week 1. %w The weekday number (0-6) with Sunday = 0. %W The week number with Monday the first day of the week (0-53). The first Monday of January is the first day of week 1. %x The date, using the locale's date format. %X The time, using the locale's time format. %y The year within century (0-99). When a century is not otherwise specified, values in the range 69-99 refer to years in the twenti- eth century (1969-1999); values in the range 00-68 refer to years in the twenty-first century (2000-2068). %Y The year, including century (for example, 1991). Some field descriptors can be modified by the E or O modifier characters to indicate that an alternative format or specification should be used. If the alternative format or specification does not exist in the current locale, the unmodified field descriptor is used. The E modifier specifies that the input string may contain alternative locale-dependent versions of the date and time representation: %Ec The locale's alternative date and time representation. %EC The name of the base year (period) in the locale's alternative representation. %Ex The locale's alternative date representation. %EX The locale's alternative time representation. %Ey The offset from %EC (year only) in the locale's alternative representation. %EY The full alternative year representation. The O modifier specifies that the numerical input may be in an alternative locale-dependent format: %Od or %Oe The day of the month using the locale's alternative numeric symbols; leading zeros are permitted but not required. %OH The hour (24-hour clock) using the locale's alternative numeric symbols. %OI The hour (12-hour clock) using the locale's alternative numeric symbols. %Om The month using the locale's alternative numeric symbols. %OM The minutes using the locale's alternative numeric symbols. %OS The seconds using the locale's alternative numeric symbols. %OU The week number of the year (Sunday as the first day of the week) using the locale's alternative numeric symbols. %Ow The number of the weekday (Sunday=0) using the locale's alternative numeric symbols. %OW The week number of the year (Monday as the first day of the week) using the locale's alternative numeric symbols. %Oy The year (offset from %C) using the locale's alternative numeric symbols. The broken-down time structure tm is defined in <time.h> as follows: struct tm { int tm_sec; /* seconds */ int tm_min; /* minutes */ int tm_hour; /* hours */ int tm_mday; /* day of the month */ int tm_mon; /* month */ int tm_year; /* year */ int tm_wday; /* day of the week */ int tm_yday; /* day in the year */ int tm_isdst; /* daylight saving time */ }; RETURN VALUE
The return value of the function is a pointer to the first character not processed in this function call. In case the input string con- tains more characters than required by the format string the return value points right after the last consumed input character. In case the whole input string is consumed the return value points to the NUL byte at the end of the string. If strptime() fails to match all of the format string and therefore an error occurred the function returns NULL. CONFORMING TO
XPG4, SUSv2, POSIX 1003.1-2001. EXAMPLE
The following example demonstrates the use of strptime() and strftime(). #include <stdio.h> #include <time.h> int main() { struct tm tm; char buf[255]; strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S", &tm); strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm); puts(buf); return 0; } GNU EXTENSIONS
For reasons of symmetry, glibc tries to support for strptime the same format characters as for strftime. (In most cases the corresponding fields are parsed, but no field in tm is changed.) This leads to %F Equivalent to %Y-%m-%d, the ISO 8601 date format. %g The year corresponding to the ISO week number, but without the century (0-99). %G The year corresponding to the ISO week number. (For example, 1991.) %u The day of the week as a decimal number (1-7, where Monday = 1). %V The ISO 8601:1988 week number as a decimal number (1-53). If the week (starting on Monday) containing 1 January has four or more days in the new year, then it is considered week 1. Otherwise, it is the last week of the previous year, and the next week is week 1. %z An RFC-822/ISO 8601 standard time zone specification. %Z The timezone name. Similarly, because of GNU extensions to strftime, %k is accepted as a synonym for %H, and %l should be accepted as a synonym for %I, and %P is accepted as a synonym for %p. Finally %s The number of seconds since the epoch, i.e., since 1970-01-01 00:00:00 UTC. Leap seconds are not counted unless leap second support is available. The GNU libc implementation does not require whitespace between two field descriptors. NOTES
In principle, this function does not initialize tm but only stores the values specified. This means that tm should be initialized before the call. Details differ a bit between different Unix systems. The GNU libc implementation does not touch those fields which are not explicitly specified, except that it recomputes the tm_wday and tm_yday field if any of the year, month, or day elements changed. This function is available since libc 4.6.8. Linux libc4 and libc5 includes define the prototype unconditionally; glibc2 includes provide a prototype only when _XOPEN_SOURCE or _GNU_SOURCE are defined. Before libc 5.4.13 whitespace (and the 'n' and 't' specifications) was not handled, no 'E' and 'O' locale modifier characters were accepted, and the 'C' specification was a synonym for the 'c' specification. The 'y' (year in century) specification is taken to specify a year in the 20th century by libc4 and libc5. It is taken to be a year in the range 1950-2049 by glibc 2.0. It is taken to be a year in 1969-2068 since glibc 2.1. SEE ALSO
time(2), getdate(3), scanf(3), setlocale(3), strftime(3) GNU
2001-11-12 STRPTIME(3)
All times are GMT -4. The time now is 11:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy