Super noob to JavaScript. Would like to create a DateObject, but am still stuck.
Wondering how I can convert a date from toDateString to custom 'MMM DD'.
Tue Mar 09 2014 > Mar 09
*basically, need to parse out the day of the week and the year.
I would like to add any additional objects and functions to this(already converting from UTC):
function dateChange(date) {
return date.toDateString();
}
Thanks in advance; looking forward to learning where I am going wrong.
You could use substr(), although it only makes sense if you're consistently using that same date format.
Or, you could use split():
var splitString = date.toDateString().split(" ");
return splitString[1]+" "+splitString[2];
If you want a more flexible library, check out Datejs (specificially, the FormatSpecifiers would be useful).
Related
my script is checking for files in several directorys. If there is e.g. an mp3 I get the creation date via
time.ctime(os.path.getctime(audio_file_path))
What I need is an Isorepresentation of the date similar to
datetime.datetime.now().isoformat()
my result at the moment looks like this:
Thu May 28 13:58:45 2020
but what I need is this:
2020-06-03T13:36:48.740664"
Is there an easy way to transform the timestamp?
os.path.getctime returns a timestamp, so you can use:
datetime.datetime.fromtimestamp(os.path.getctime(audio_file_path)).isoformat()
I get result as follow when I execute a sql which includes now()function in VoltDB
select * from test2;
C1
---------------------------
2019-06-29 07:13:38.050000
But,I get another time when I execute a shell command 'date'
[root#localhost config]# date
Sat Jun 29 03:30:09 EDT 2019
How can I make them same?
VoltDB stores time in the UTC timezone. If you want to change this to EDT or any other timezone, you should be able to do it using a variety of methods and languages.
For example, in java you might use VoltTableRow.getTimestampAsSqlTimestamp(0) to get the value of a timestamp row. You could then use DateFormat to convert this to a particular timezone.
You can read more about the "getTimestampAsSqlTimestamp" method here: https://docs.voltdb.com/javadoc/java-client-api/org/voltdb/VoltTableRow.html
Full Disclosure: I work at VoltDB.
just stumbled upon one funny issue in Node.js & MacOs - basically if you run instance of node with set timezone var in .env file, variable TZ, and you local machine is in different timezone, the Date creation is f**d up in the wort way possible, so that:
Timezone is set correctly
But when you create a new Date, the timezone is not reflected
see this example, (my machine is in CET, timezone Europe/Prague) :
~/
11:13 (CET) $ node
new Date()
2019-02-04T10:30:23.053Z # Europe/Prague
new Date().getTimezoneOffset()
-60 # Europe/Prague
.exit
vs. set timezone in TZ env
✔ ~/
11:30 (CET) $ TZ=America/New_York node
new Date()
2019-02-04T10:31:40.968Z # 10:31! :D
new Date().getTimezoneOffset()
300 # America/New_York!
So it is not possible to create a correct date then, since all JS libs like moment uses in the end Date object.
My take is that you should never ever set TZ var while running Node.js server, do I miss something?
Your examples look correct to me. I suspect that your notion of the way the computer keeps track of, and shows, time is upside-down. Your comments imply that you think TZ is used to derive UTC from the computer's internal clock. That's not how it works.
Internally, your Mac is using UTC time. Your Mac is not "in CET". It is "in UTC with a default TZ setting of CET". TZ controls how a local time is derived from the underlying UTC when a local timestamp or timezone offset is required. If you change TZ then local timestamps will be shown in the new timezone, but the computer's internal timekeeping in UTC is not affected.
Specifically for your example, new Date() gets you a Date object representing the current time. The default string representation of a Date object is the UTC time. In your example you can see that this time, shown as the result of the new Date() call:
2019-02-04T10:30:23.053Z # Europe/Prague
is a UTC time because its timezone is shown as Z, indicating UTC. (One way to remember that meaning is that "Z" is for "Zero offset from UTC". This format is sometimes called "Zulu" time because "Zulu" is the ICAO phonetic alphabet's code word for "Z".) If your comment is claiming that this is a local Europe/Prague timestamp then the comment is incorrect.
The result of the second new Date():
2019-02-04T10:31:40.968Z # 10:31! :D
is also shown in UTC and is about a minute later than the first result. It doesn't matter that TZ for this process is different, because TZ does not affect UTC timestamps.
To see the local time, which is calculated by applying the TZ setting to the UTC time obtained from the computer's clock, use the toLocaleString method on the Date object. If you repeat your test using that method you'll see something like this:
$ env TZ=Europe/Prague node
> now = new Date()
2019-02-04T20:26:40.408Z
> now.toLocaleString()
'Mon Feb 04 2019 21:26:40 GMT+0100 (CET)'
$ env TZ=America/New_York node
> now = new Date()
2019-02-04T20:27:12.438Z
> now.toLocaleString()
'Mon Feb 04 2019 15:27:12 GMT-0500 (EST)'
which looks perfectly reasonable.
BTW, Node, or JavaScript, isn't doing anything unusual here. This is the way it works for everything. For example, the date command:
$ env TZ=Europe/Prague date
Mon Feb 4 21:54:49 CET 2019
$ env TZ=America/New_York date
Mon Feb 4 15:54:51 EST 2019
You can solve your issue with moment-timezone node package. below code may help you to resolve your issue.
const moment = require('moment-timezone')
function convertDate(dateInUTCFormat) {
return new Promise((resolve, reject) => {
var dec = moment(dateInUTCFormat);
var normalDate = dec.tz('Asia/Kolkata').format('YYYY-MM-DD'); // GMT +5:30 (India)
resolve(normalDate);
})
}
Also you need to pass the date format that you get in UTC format for eg 2019-02-04T10:30:23.053Z to convertDate function and pass your timezone as i passed my timezone Asia/Kolkata . May this will help you to find your solution.
I have a date in a the %c format (could be any other) and I need to use it in the date command. %c is NOT the American format. It is the German one because it's a German server. This also did not work properly on an American server. (Locales set to German or American)
This does not work (error included):
user#server:~$ NOW=$(date +%c); echo $NOW
Do 19 Dez 2013 22:33:28 CET
user#server:~$ date --date="$NOW" +%d/%m/%Y
date: ungültiges Datum „Do 19 Dez 2013 22:33:28 CET“
(date: ungültiges Datum „Do 19 Dez 2013 22:33:28 CET“ = date: invalid date „Do 19 Dez 2013 22:33:28 CET“)
The difficulty is that I don't know which locale or even whci dateformat will be used later since the user can set their own format. So a simple specific parsing solution ist not really going to work!
But how do I do it?
To gerneralize the issue:
If I have a date format format1 (which could be any or at least one that can be reversed) I can use date to get a formatted date. But if I want to format it to another date (format2) how do I do it?
Any solution using anything else than the coreutils is pointless since I am trying to develop a bash script for as many unix machines as possible.
DATE=$(date "+$format1")
date --date="$DATE" "+$format2" # Error in most cases!
This is needed because I have a command which the user can give a date format. This date string is going to be displayed. But in a later step I need to convert this date string into another fixed one. I can manipulate the whcih format the command will get and I can maniplulate the output (or what the user will see).
I cannot run the command twice because it is very time consuming.
Update:
I have found something like a solution:
# Modify $user_format so it can be parsed later
user_format="$user_format %s"
# Time consuming command which will print a date in the given format
output=$(time_consuming_command params "$user_format" more params)
# This will only display what $user_format used to be
echo ${output% *}
# A simple unix timestamp parsing ("${output##* }" will only return the timestamp)
new_formated_date=$(date -d "1970-01-01 ${output##* } sec UTC" "+$new_format")
This is working and might be helpful to others. So I will share this with you.
Not possible with --date as of GNU coreutils 8.22. From the date manual:
‘-d datestr’
‘--date=datestr’
Display the date and time specified in datestr instead of the current
date and time. datestr can be in almost any common format. It can
contain month names, time zones, ‘am’ and ‘pm’, ‘yesterday’, etc. For
example, --date="2004-02-27 14:19:13.489392193 +0530" specifies the
instant of time that is 489,392,193 nanoseconds after February 27,
2004 at 2:19:13 PM in a time zone that is 5 hours and 30 minutes east
of UTC.
Note: input currently must be in locale independent format. E.g., the
LC_TIME=C below is needed to print back the correct date in many
locales:
date -d "$(LC_TIME=C date)"
http://www.gnu.org/software/coreutils/manual/html_node/Options-for-date.html#Options-for-date
Note it says that the input format cannot be in a locale-specific format.
There may be other libraries or programs that would recognize more date formats, but for a given date format it would not be difficult to write a short program to convert it to something date recognizes (for example, with Perl or awk).
Why don't you store the time as unixtime (ie milliseconds since 1st of january 1970) Like 1388198714?
The requested exercise in trying to parse all date formats from all around the world as a one shot bash script without reasonable dependecies is slightly ridiculous.
You may use libdatetime-format-flexible-perl.
#!/usr/bin/perl
use DateTime::Format::Flexible;
my $date_str = "So 22 Dez 2013 07:29:35 CET";
$parser = DateTime::Format::Flexible->new;
my $date = $parser->parse_datetime($date_str);
print $date
Default output will be 2013-12-22T07:29:35, but since $date is not a regular string but object, you can do something like this:
printf '%02d.%02d.%d', $date->day, $date->month, $date->year;
Also date behavior probably should be considered as a bug. I think so, because date in the same format but in russian is parsed correctly.
$ export LC_TIME=ru_RU.UTF-8
$ NOW="$(date "+%c")"
$ date --date="$NOW" '+%d.%m.%Y'
22.12.2013
If you meant the formatting is wrong, I think what you want is:
NOW=$(date +%c)
date --date="$NOW" +%d/%m/%Y
note the lowercase %d and %m.
Locally, this is what I get:
root#server2:~# NOW=$(date +%c)
root#server2:~# date --date="$NOW" +%d/%m/%Y
19/12/2013
We have to identify and extract the date from given samplese
Oct 4 07:44:45 cli[1290]: PAPI_Send: To: 7f000001:8372 Type:0x4 Timed out.
Oct 4 08:16:01 webui[1278]: USER:admin#192.168.100.205 COMMAND:<wlan ssid-profile "MFI-SSID" > -- command executed successfully
Oct 4 08:16:01 webui[1278]: USER:admin#192.168.100.205 COMMAND:<wlan ssid-profile "MFI-SSID" opmode opensystem > -- command executed successfully
Here the main problem is, Date format is versatile. it may be "oct 4 2004" or "oct/04/2004" etc
parsing is best way to handle such a problems.
so learn about parsing techniques then use them on your project and enjoy. Appropriate design pattern for an event log parser?