I'm attempting to use moment.js to format Apache log timestamps (14/Jun/2015:11:05:54 -0700) as proper MySQL DATETIME format.
Getting the MySQL DATETIME format is a breeze. But moment seems to only auto-detect ISO 8601 formats. My Node.js console output when passing in the Apache log's timestamp into moment:
> moment( '08/Jun/2015:15:03:29' ).format("YYYY-MM-DD HH:mm:ss")
'Invalid date'
I can't figure out a way to parse the Apache log timestamp without doing a ton of string splitting in JavaScript. Is there a clean way of loading the Apache log timestamp into moment? Or perhaps into the Date object?
NOTE: Changing the Apache log's timestamp format wouldn't work for my case, because I'm working with old log files, not new ones.
You need to tell moment what format the string is in so it can parse it.
Something like:
moment( '08/Jun/2015:15:03:29', 'DD/MMM/YYYY:HH:mm:ss Z').format("YYYY-MM-DD HH:mm:ss")
Related
I'm trying to insert start and end time like entry and exit time logic into Mongodb from node js.
My local system's timezone is IST. +5:30 offset for both client and sever.
So now follow this example.
entryDate: 15-10-2018,
start: 15-10-2018 01:00:00,
end: 15-10-2018 05:00:00
Now since i'm on same timezone on client and server, it won't change my date value and create new object.
Then i'm storing this data into MongoDB. As you know that mongo db will convert date to UTC and then stores it. So in my DB it will look like this.
entryDate: ISODate("2018-10-15T00:00:00Z"),
start: ISODate("2018-10-14T19:30:26Z"),
end: ISODate("2018-10-14T23:30:26Z")
So now if I want to search a query that should give me records for dates and time between 15 to 16 let's say then i won't get any data.
Now consider this case. I moved app on to the server. Now servers timezone is in UTC. So every date I pass will be converted directly in UTC so it'll insert same data like before.
Now difference here is when i want to search date range in IST timezone then it will give me the exact date to search like (15-10-2018 00:00:00) to (16-10-2018 00:00:00).
Where on server it will convert this date to UTC and can give me one day less or may be higer time like (14-10-2018 18:30:00) to (15-10-2018 18:30:00). So from the start my query is wrong.
So how to solve this issue? Thank you.
After doing some research. I have a solution.
Get your date on the client side.
Convert that date to UTC date and pass date string to the server. Like 01-01-2018 10:00:00. (Note: we are just converting date, not the timezone)
On the server side create a date object from the provided date that will convert the date to UTC by default with that same time.
Store the date on the server side with MongoDB. Since the date is in UTC it won't convert the date again and store it as it is.
Now from the server, you will get the date in UTC format which will be like '2018-01-01 10:00:00'.
Now on the client side, create a date object from this date. This will convert your date to actual date that you passed.
This solved my problem.
Is there a function which will help us to write the date using the Locale settings in Javascript.For example in my machine as per the Locale settings the format is M/D/YYYY so in my program the date should be printed in this format. If i change my the Locale settings in computer format to YYYY-MM-DD then my program should automatically print in this format without hardcoding the format. This is the expected behavior of my program. As some of the suggestions given on using toLocaleDateString method, On using toLocaleDateString it always returns in M/D/YYYY format only even then the computer Date format is changed to YYYY-MM-DD. I searched in internet i don't find any proper answer please guide me to do this. Any help is appreciated.
Am using:
mongoDB to fetch data to the server which is based on: node.js and express. Template engine is PUG aka. Jade.
in each document stored inside the DB there is a date attribute stored as UTC.
Question/problem.
I need to convert UTC to the clients local-time.
Solving alternatives:
Using Aggregation in MongoDB
Using moment.js library and make the conversion in node
Pass moment.js object inside the view and make the conversion their,
as suggested in this post:
How can I format a date coming from MongoDB?
Using moment.js to format an ISO 8601 date string to local time can be achieved like this:
let moment = require('moment');
let localizedDate = moment('2014-06-01T12:00:00Z').format('LT');
console.log(localizedDate);
I am assuming that , If I run this query on
> (new Date()).getTime()
1443104144268
on Node console or JS console then I will get this timeStamp.
As per my understanding, irrespective of Location of console or timezone, this timestamp is always same. If at one particular instance, everybody in the world run this statement, then everybody will get same timestamp.
In my project, I need to convert this timeStamp (1443104144268) to YYYY-MM-DD (2015-09-24) format.
But I do not want to use any existing Date/Time library of System library.
Do anybody know how to convert this timestamp to YYYY-MM-DD (2015-09-24) format without using Date function in JavaScript. ?
I had a problem on extjs date. Seem by default it using computer date to create a date.
I don't check up my pc and post the date.Then i found it based on American
M/d/yyyy.
After i change the system regional setting date to d-m-Y.Everthing work fine.
So anybody know how extjs get client date format ?
e.g 'YYYY-mm-dd' or 'M/d/yyyy' .
Since i need to parse the format from extjs code to php to mysql date format.
I try to find stackoverflow and site but seem not found out,
You can convert date to String when transfer it so that you can set the date format by yourself.
Actually the default format used is 'm/d/y', and you can change the format self, and I don't think so the ext can get the client date format and change the format consistently.
dont mind your regional setting
you have two options
in your server you convert to string and change format
yourDate.ToString("MMM dd, yyyy")
see here or here for standard and custom format
or in your extjs code you use
renderer: Ext.util.Format.dateRenderer('d-M-Y'),
see here for available format strings.