Update couch db docment date format - javascript

Is there way I can update all the date field format existing in documents of couch db
change format from
DateTime : "07-29-2017 19:07:23"
to
DateTime : "2017-07-29 19:07:23"

There's no automated way to do this, aside from writing a script that updates each of your documents.
An alternative, depending on your exact situation, might be to use a view to manipulate the data as it's being read. The view could detect the existing date format, and if it's the old one, convert it before displaying the document.
This would change the way you query the data, though--you'd have to request the new view, which could obviously be a deal-breaker in some scenarios.

First, you need a way to apply a function to every document. To do so, I suggest that you use pouchdb-migrate.
Finally, you only have to define your function and integrate it into pouchdb-migrate. You can either parse the date and convert it or simply do some string manipulation.

Related

MongoDB current date in save method

How to make MongoDB fill a field with current UTC DateTime on save method?
I know there is $currentDate operator, but it's only available in update method.
It sounds like you're trying to get access to the time on the mongo server for the insert, right? Mongo _ids encode the timestamp of their creation -- would that work for your needs? https://docs.mongodb.com/manual/reference/method/ObjectId/
Otherwise, I think you might need to provide a date from your application code.

How input date as string in javascript client, pass to python server, and store as datetime in myql?

I want my users to input date as a string.
The date is passed via a Python backend to a MySQL database where it is stored as datetime.
What is a way of doing this? To clarify, I am asking what kinds of conversions I should do, where, and using what packages.
Now a lot of things in this domain depends on your use-case but I'll take a shot. I'm guessing you pass your data to the server after JSON.stringifying it?
Now we have data on the server. You get everything as json strings, do a json loads and convert them to python strings(unicodes) on your server. From here on, things are easy except for one single problem
You'll have to know the format of your date beforehand
Why?
Because you'll have to do a strptime on your date. A simple example of converting a string date to datetime object is -
from datetime import datetime
dt = datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")
# strptime("date string", "format of date")
The example can be found just below this.
The format table can be found here (which is quite handy, I'd bookmark it)
I hope that makes some if not complete sense

How to Deal with String/Date Attributes in Angular

Which strategy do you guys use to deal with date attributes on your models which comes from json response on http requests ?
The Date attributes comes in string format and it troubles me when i'm using date components(like a Datepicker).
So, to fix this, usually I convert the String attribute into a date, like this:
person.birthday = new Date(person.birthday);
But it seems to be bad code. Besides, everytime I create a new date attribute I have to repeat the code above.
How do you guys deal with this situation ?
The simplest way I've found is to deal with dates as Timestamps.
This means you store a number, and when you request it, all you have to do is to make a
let d = new Date(myTS);
You can find some problems to that, such as the timezone depending on the client.
Otherwise, you can use Moment, which is made to deal with this kind of problem.

String to Date mongodb using node.js and moment.js

In my application i allow user to set a date using a date-picker component.
My issue is when trying to convert string date to UTC/ISO date format the conversion is not happening properly, see the below example.
Example:
User Selected String Date : 01/09/2015 (DD/MM/YYYY)
While im using moment.js to convert the above date to native JS date, See below:
moment(req.body.datePicker,'DD/MM/YYYY')
All good till here, but when the data stored in db the date is getting reduced by 1 day. I have no idea where its getting messed up.
I have managed to create a re-producable scenario using jsfiddle-example please have a look for a better understanding.
My assumption:
when i see the db collection the date is getting stored with a default time: 18:30:00.00Z
this could be one of the reason why the dates are getting changed.
Your problem is timezone.
In my case 01/09/2015 gives 2015-01-08T23:00:00 which is only -1 hour since my timezone is GMT+1
Finally after doing some research i myself found the solution or a hack to fix this issue, it may not be the correct way of doing it but for the current scenario it worked like a charm.
Solution:
All i did was just created a full ISO/UTC date string hard-coding the time to zero. moment(req.body.datePicker,'DD/MM/YYYY').format('YYYY-MM-DD HH:mm:ss.000').toString()+'Z' by doing this the default time will always set to Zero and since the string is a fully qualified ISO/UTC standard format, mongoDB will not try to alter it.
Working example:
http://jsfiddle.net/r42jg/1004/
If any one has a better solution, the please post here so we can improvise this better.

Manipulate DateTime and retain original format

I have a DateTime string which I can assume is in a parsable format using Date.parse() or a 3rd party library. The format could differ based on a user's location and settings. For example, here are some strings I could see: "2014/08/19 16:10:20" or "08/19/2014 4:10:20 PM".
I would like to manipulate the time, say by adding 30 minutes, but also preserve the original format string when I present the new time to the user. I suppose I'm looking for something like a parse function that gives you a DateTime along with the format string it detected. Is there an easy way to accomplish this? 3rd party libraries are welcome, with a preference towards jQuery since I'm already using that.
Note: I don't know what the format is going to be before I parse the datetime string. It can be one of many different formats. (However I probably could build a list of possible format strings.)

Categories