Change the date and time format in UNIX script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change the date and time format in UNIX script.
# 1  
Old 06-06-2014
Change the date and time format in UNIX script.

Hi, I am extracting a date string from the source file like this :
Code:
06/05/2014 16:04:00

I want to change it to
Code:
  05-JUN-14 04.05.00.000000000 PM

I basically store the date in a variable. I got solutions to change date in dd-mmm-yyyy format using tr but I guess it works only with the "date" command and not with a variable that stores the old date.

The command is
Code:
 date '+%d-%h-%Y | tr 'a-z' 'A-Z'

But it is not working when I replace "date" with my variable 'tdate'.
Is there a way in UNIX to get the date and timestamp from a stored variable?


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks

Last edited by vbe; 06-06-2014 at 11:09 AM..
# 2  
Old 06-06-2014
What have you tried so far?

Have you read the formatting options in the date manual page?
Code:
man date

What are you trying to achieve? If it's just set the alpha characters (month only) comes out as all capitals? If it's in a variable, you could:-
Code:
tdate="05-Jun-14 04.05.00.0000000000 PM"
typeset -u tdate
echo "$tdate"



Does that do it?

Robin
# 3  
Old 06-06-2014
Are you adding a minute to get from 16:04:00 to 04.05.00.000000000 PM?
And, I don't think you can
Quote:
change date in dd-mmm-yyyy format using tr
. If you're lucky, you can use the date command as rbatte1 proposes, if not, you need to work on every single field...
# 4  
Old 06-06-2014
Quote:
Originally Posted by Varshha
The command is
Code:
 date '+%d-%h-%Y' | tr 'a-z' 'A-Z'

But it is not working when I replace "date" with my variable 'tdate'.
Is there a way in UNIX to get the date and timestamp from a stored variable?

Code:
echo $tdate | tr 'a-z' 'A-Z'

BTW, You have unbalanced quotes in your original command. Highlighted in RED.
# 5  
Old 06-07-2014
What I want to do is convert a date-time format
Quote:
06/05/2014 16:04:00
to
Quote:
05-JUN-14 04.04.00.000000000 PM
As I earlier said I cannot use the "date" command as this date is not the system date but a value that I am extracting from the input file like this :

Code:
tdat=`head -1 ${INPUTFILE} | cut -f9-10 -d'|' | sed 's/|/ /g' | cut -c1-19`

So basically I want to covert the format of this tdat variable.

What have I tried so far? I have used this variable and queried it in an oracle database which works fine, but I want to do this purely in UNIX.

---------- Post updated at 12:34 AM ---------- Previous update was at 12:33 AM ----------

What I want to do is convert a date-time format
Quote:
06/05/2014 16:04:00
to
Quote:
05-JUN-14 04.04.00.000000000 PM
As I earlier said I cannot use the "date" command as this date is not the system date but a value that I am extracting from the input file like this :

Code:
tdat=`head -1 ${INPUTFILE} | cut -f9-10 -d'|' | sed 's/|/ /g' | cut -c1-19`

So basically I want to covert the format of this tdat variable.

What have I tried so far? I have used this variable and queried it in an oracle database which works fine, but I want to do this purely in UNIX.
# 6  
Old 06-07-2014
Quote:
As I earlier said I cannot use the "date" command as this date is not the system date but a value that I am extracting from the input file like this :
That's irrelevant if your date supports the -d --date option

Code:
date -d "06/05/2014 16:04:00" "+%d-%b-%y %I.%M.%S.%N %p"

Code:
FORMAT="+%d-%b-%y %I.%M.%S.%N %p"
date -d "$tdat" "$FORMAT"

# 7  
Old 06-07-2014
Smilie My system does not support -d Smilie


Quote:
date "06/05/2014 16:04:00" "+%d-%b-%y %I.%M.%S.%N %p"
date: 0551-402 Invalid character in date/time specification.
Usage: date [-u] [+"Field Descriptors"]
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change date format in am/pm in csv files using UNIX

Hi All, I'm new to forum good to hear all. I stuck in converting date format in csv file using unix csv file contains as below ,750,0000000000000000GCJR, ,06/22/2016 14:48:44 I want to convert into as below ,750,0000000000000000GCJR, ,06/22/2016 02:48:44 PM Please reply asap..... (22 Replies)
Discussion started by: Raghureds
22 Replies

2. Shell Programming and Scripting

Script to change date/time

Hi everybody! I need to perform a task with a script but I have no idea how to do it, I hope someone could help me: - on my linux pc I have many folders with movies, tv shows, toons, ecc. They are shared by a dlna server to my panasonic tv where I can browse and see them. The problem is that... (6 Replies)
Discussion started by: Torquemada
6 Replies

3. Shell Programming and Scripting

Date format change in UNIX .dat file

Hi, I need help to convert the date format in .DAT file in unix. I want to convert 10@@|SWIFT MT568 Extract@@|Apr 14 2014 5:47:52:563PM@@|Apr 14 2014 4:33:47:663PM@@||##| into 10@@|SWIFT MT568 Extract@@|04/14/2014/ 5:47:52:563PM@@|04/14/2014 4:33:47:663PM@@||##| Appreciate... (18 Replies)
Discussion started by: karthikengox
18 Replies

4. UNIX for Dummies Questions & Answers

Rename all Files in a UNIX Directory from one date format to another date format

Hi Unix Gurus, I would like to rename several files in a Unix Directory . The filenames can have more than 1 underscore ( _ ) and the last underscore is always followed by a date in the format mmddyyyy. The Extension of the files can be .txt or .pdf or .xls etc and is case insensitive ie... (1 Reply)
Discussion started by: pchegoor
1 Replies

5. Shell Programming and Scripting

Change date format in shell script

Plz help me To display date in the mm/dd/yyyy. Eg. if date is 28-09-2012 the output of the shell script should be date 09/28/2012. (1 Reply)
Discussion started by: shivasaini
1 Replies

6. UNIX for Dummies Questions & Answers

Shell Scripts - shows today’s date and time in a better format than ‘date’ (Uses positional paramete

Hello, I am trying to show today's date and time in a better format than ‘date' (Using positional parameters). I found a command mktime and am wondering if this is the best command to use or will this also show me the time elapse since 1/30/70? Any help would be greatly appreciated, Thanks... (3 Replies)
Discussion started by: citizencro
3 Replies

7. OS X (Apple)

how to change date and time format in menu bar

Can someone please tell me how to change the time and date format in the menu bar. I get to System Preferences -- Language and Text, format, customize, but I can't figure out what to do next. I want to change, say from Tue May 4 to 5 4 2010. How the heck do I do this? (5 Replies)
Discussion started by: Straitsfan
5 Replies

8. Shell Programming and Scripting

help for change date format script

Someone can help me write a script for change date fromat from "Feb 4 18:44:03 2009" to 2009020418 ? from "Mar 17 16:44:03 2009" to 2009031716 ? (4 Replies)
Discussion started by: pccwtest
4 Replies

9. Shell Programming and Scripting

Convert Epoch time format to normal date time format in the same file

I have a file named "suspected" with series of line like these : {'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '219.78.120.166', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 8291, 'time': 1226506312L, 'serverhostname': ''} {'protocol': 17, 'service': 'BitTorrent... (3 Replies)
Discussion started by: rk4k
3 Replies

10. Shell Programming and Scripting

script to change the date format in a file

i have many files with date format of 6-9-2008 and i want a script that can change the format to 2008-06-09 Thanks (15 Replies)
Discussion started by: shehzad_m
15 Replies
Login or Register to Ask a Question