How to read dd/mm/yyyy date string in date -d UNIX command?
I am trying to dynamically convert one column in text file containing date in dd/mm/yyyy hh:mm:ss format to yy-mm-dd hh:mm:ss. Following command is working but giving output in yy-dd-mm and NOT yy-mm-dd as I need.
echo $(date -d "8/11/2018 06:20:57 AM" "+%y-%m-%d %H:%M:%S")
Output:
18-08-11 06:20:57
Moreover, this is not working for dates > 12 as it is treating first digit as month not date as below:
echo $(date -d "28/11/2018 06:20:57 AM" "+%y-%m-%d %H:%M:%S")
Output:
date: invalid date ‘28/11/2018 06:20:57 AM’
Is there any way to make it work?
shell-script date
New contributor
Deepak Malhotra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I am trying to dynamically convert one column in text file containing date in dd/mm/yyyy hh:mm:ss format to yy-mm-dd hh:mm:ss. Following command is working but giving output in yy-dd-mm and NOT yy-mm-dd as I need.
echo $(date -d "8/11/2018 06:20:57 AM" "+%y-%m-%d %H:%M:%S")
Output:
18-08-11 06:20:57
Moreover, this is not working for dates > 12 as it is treating first digit as month not date as below:
echo $(date -d "28/11/2018 06:20:57 AM" "+%y-%m-%d %H:%M:%S")
Output:
date: invalid date ‘28/11/2018 06:20:57 AM’
Is there any way to make it work?
shell-script date
New contributor
Deepak Malhotra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
date is very flexible on the output but the input is very specific. I'd suggest to transform the date before reaching date. May be bring in them from SQL functions or Javascript (depending on the source). If there is no source (like from a file), I'd suggest sed or awk
– Juan
1 hour ago
... or use something like Perl's Time::Piece with itsstrptime/strftimeimplementations
– steeldriver
1 hour ago
orphp -r "command"...
– Juan
1 hour ago
add a comment |
I am trying to dynamically convert one column in text file containing date in dd/mm/yyyy hh:mm:ss format to yy-mm-dd hh:mm:ss. Following command is working but giving output in yy-dd-mm and NOT yy-mm-dd as I need.
echo $(date -d "8/11/2018 06:20:57 AM" "+%y-%m-%d %H:%M:%S")
Output:
18-08-11 06:20:57
Moreover, this is not working for dates > 12 as it is treating first digit as month not date as below:
echo $(date -d "28/11/2018 06:20:57 AM" "+%y-%m-%d %H:%M:%S")
Output:
date: invalid date ‘28/11/2018 06:20:57 AM’
Is there any way to make it work?
shell-script date
New contributor
Deepak Malhotra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I am trying to dynamically convert one column in text file containing date in dd/mm/yyyy hh:mm:ss format to yy-mm-dd hh:mm:ss. Following command is working but giving output in yy-dd-mm and NOT yy-mm-dd as I need.
echo $(date -d "8/11/2018 06:20:57 AM" "+%y-%m-%d %H:%M:%S")
Output:
18-08-11 06:20:57
Moreover, this is not working for dates > 12 as it is treating first digit as month not date as below:
echo $(date -d "28/11/2018 06:20:57 AM" "+%y-%m-%d %H:%M:%S")
Output:
date: invalid date ‘28/11/2018 06:20:57 AM’
Is there any way to make it work?
shell-script date
shell-script date
New contributor
Deepak Malhotra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Deepak Malhotra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 1 hour ago
Jeff Schaller
43.8k1161141
43.8k1161141
New contributor
Deepak Malhotra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 1 hour ago
Deepak MalhotraDeepak Malhotra
31
31
New contributor
Deepak Malhotra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Deepak Malhotra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Deepak Malhotra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
date is very flexible on the output but the input is very specific. I'd suggest to transform the date before reaching date. May be bring in them from SQL functions or Javascript (depending on the source). If there is no source (like from a file), I'd suggest sed or awk
– Juan
1 hour ago
... or use something like Perl's Time::Piece with itsstrptime/strftimeimplementations
– steeldriver
1 hour ago
orphp -r "command"...
– Juan
1 hour ago
add a comment |
2
date is very flexible on the output but the input is very specific. I'd suggest to transform the date before reaching date. May be bring in them from SQL functions or Javascript (depending on the source). If there is no source (like from a file), I'd suggest sed or awk
– Juan
1 hour ago
... or use something like Perl's Time::Piece with itsstrptime/strftimeimplementations
– steeldriver
1 hour ago
orphp -r "command"...
– Juan
1 hour ago
2
2
date is very flexible on the output but the input is very specific. I'd suggest to transform the date before reaching date. May be bring in them from SQL functions or Javascript (depending on the source). If there is no source (like from a file), I'd suggest sed or awk
– Juan
1 hour ago
date is very flexible on the output but the input is very specific. I'd suggest to transform the date before reaching date. May be bring in them from SQL functions or Javascript (depending on the source). If there is no source (like from a file), I'd suggest sed or awk
– Juan
1 hour ago
... or use something like Perl's Time::Piece with its
strptime/strftime implementations– steeldriver
1 hour ago
... or use something like Perl's Time::Piece with its
strptime/strftime implementations– steeldriver
1 hour ago
or
php -r "command" ...– Juan
1 hour ago
or
php -r "command" ...– Juan
1 hour ago
add a comment |
1 Answer
1
active
oldest
votes
You might want to pre processes it first. My best shot is with sed
echo "28/11/2018 06:20:57 PM" | sed -E "s/([0-9]+)/([0-9]+)/([0-9]+) ([0-9]+):([0-9]+):([0-9]+) ([APM]{2})/3/2/1 4:5:6 7/g" | xargs -I II date -d II "+%y-%m-%d %H:%M:%S"
Great ! It works, Thank you so much . :) I used date because I had multiple column with dates different format in every column , it was working fine for other columns except this column because it has different date format with PM and AM in it.
– Deepak Malhotra
23 mins ago
You are welcome. Don't forget to vote up ;)
– Juan
20 mins ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Deepak Malhotra is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f508613%2fhow-to-read-dd-mm-yyyy-date-string-in-date-d-unix-command%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You might want to pre processes it first. My best shot is with sed
echo "28/11/2018 06:20:57 PM" | sed -E "s/([0-9]+)/([0-9]+)/([0-9]+) ([0-9]+):([0-9]+):([0-9]+) ([APM]{2})/3/2/1 4:5:6 7/g" | xargs -I II date -d II "+%y-%m-%d %H:%M:%S"
Great ! It works, Thank you so much . :) I used date because I had multiple column with dates different format in every column , it was working fine for other columns except this column because it has different date format with PM and AM in it.
– Deepak Malhotra
23 mins ago
You are welcome. Don't forget to vote up ;)
– Juan
20 mins ago
add a comment |
You might want to pre processes it first. My best shot is with sed
echo "28/11/2018 06:20:57 PM" | sed -E "s/([0-9]+)/([0-9]+)/([0-9]+) ([0-9]+):([0-9]+):([0-9]+) ([APM]{2})/3/2/1 4:5:6 7/g" | xargs -I II date -d II "+%y-%m-%d %H:%M:%S"
Great ! It works, Thank you so much . :) I used date because I had multiple column with dates different format in every column , it was working fine for other columns except this column because it has different date format with PM and AM in it.
– Deepak Malhotra
23 mins ago
You are welcome. Don't forget to vote up ;)
– Juan
20 mins ago
add a comment |
You might want to pre processes it first. My best shot is with sed
echo "28/11/2018 06:20:57 PM" | sed -E "s/([0-9]+)/([0-9]+)/([0-9]+) ([0-9]+):([0-9]+):([0-9]+) ([APM]{2})/3/2/1 4:5:6 7/g" | xargs -I II date -d II "+%y-%m-%d %H:%M:%S"
You might want to pre processes it first. My best shot is with sed
echo "28/11/2018 06:20:57 PM" | sed -E "s/([0-9]+)/([0-9]+)/([0-9]+) ([0-9]+):([0-9]+):([0-9]+) ([APM]{2})/3/2/1 4:5:6 7/g" | xargs -I II date -d II "+%y-%m-%d %H:%M:%S"
answered 56 mins ago
JuanJuan
201110
201110
Great ! It works, Thank you so much . :) I used date because I had multiple column with dates different format in every column , it was working fine for other columns except this column because it has different date format with PM and AM in it.
– Deepak Malhotra
23 mins ago
You are welcome. Don't forget to vote up ;)
– Juan
20 mins ago
add a comment |
Great ! It works, Thank you so much . :) I used date because I had multiple column with dates different format in every column , it was working fine for other columns except this column because it has different date format with PM and AM in it.
– Deepak Malhotra
23 mins ago
You are welcome. Don't forget to vote up ;)
– Juan
20 mins ago
Great ! It works, Thank you so much . :) I used date because I had multiple column with dates different format in every column , it was working fine for other columns except this column because it has different date format with PM and AM in it.
– Deepak Malhotra
23 mins ago
Great ! It works, Thank you so much . :) I used date because I had multiple column with dates different format in every column , it was working fine for other columns except this column because it has different date format with PM and AM in it.
– Deepak Malhotra
23 mins ago
You are welcome. Don't forget to vote up ;)
– Juan
20 mins ago
You are welcome. Don't forget to vote up ;)
– Juan
20 mins ago
add a comment |
Deepak Malhotra is a new contributor. Be nice, and check out our Code of Conduct.
Deepak Malhotra is a new contributor. Be nice, and check out our Code of Conduct.
Deepak Malhotra is a new contributor. Be nice, and check out our Code of Conduct.
Deepak Malhotra is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f508613%2fhow-to-read-dd-mm-yyyy-date-string-in-date-d-unix-command%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
date is very flexible on the output but the input is very specific. I'd suggest to transform the date before reaching date. May be bring in them from SQL functions or Javascript (depending on the source). If there is no source (like from a file), I'd suggest sed or awk
– Juan
1 hour ago
... or use something like Perl's Time::Piece with its
strptime/strftimeimplementations– steeldriver
1 hour ago
or
php -r "command"...– Juan
1 hour ago