How to Deal with String/Date Attributes in Angular - javascript

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.

Related

Should I use `Date()` to store a date without time in javascript

I need to store in the backend a date in the format yyyy-mm-dd;
I've initially used in the frontend a js Date() object to store it, and dropping the time and timezone info while sending it to the backend; but that caused a "timezone issue" when parsing back the yyyy-mm-dd string into Date();
even if it's possible to workaround that issue I was wondering if using a simple String wouldn't be better; is there any drawback that now I cannot see and so I should use a Date anyway?
Similar to other comments, I would also recommend sticking with Date instead of strings to prevent browser differences and for ease of use. One note is when setting the date, don't forget to account for day-light savings which might cause a database crash if not accounted for.
CSS-Tricks: Everything you need to know about date in javascript

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

JavaScript Date Object includes "T" character in browser, but not in MongoDB

I've seen this post already, and it didn't quite shed much light on what is going on.
I have a mongo server, that stores information about classes, what times and days they meet and what not. I also have an express server, that interacts with and returns this data.
When I look at the data in Robo3T (a mongo data viewer), it is formatted - as it should be - like so:
2018-09-07 04:00:00.000Z
But when the data comes back to the server, it comes back like this: 2018-09-07T04:00:00.000Z
What is causing this? In no way do i make any attempt to format this data before I display it. I also checked the server output, which returns it the same way:
I guess my question is, why is this happening? and is this impacting my ability to query results from mongo based on a date range? Any assistance would be much appreciated, thank you.
Robo 3T just happens to display dates that way. This does not mean that it actually is stored that way. When you switch the view to "text mode", you will see that it actually is ISODate("2018-09-07T04:00:00.000Z") (with the T).
This format is actually the date format string that is used in JavaScript. And yes, the T is required. See What are valid Date Time Strings in JavaScript?

Update couch db docment date format

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.

Dynamic typing in JavaScript: Is this a good idea?

I haven't done a lot of coding in dynamically typed languages such as JavaScript until recently, and now that I'm beginning to understand what's possible, I'm starting to wonder what's a good idea and what's not. Specifically, I am unsure as to whether changing the type of a variable as a function progresses through a sequence of operations is considered good practice.
For example, I have a bunch of files containing dates as strings. I'm using front-matter to extract date attributes and storing them within an object representing the original file. The strings themselves aren't very consistent, so I'm then using Moment.js to parse them, and storing the result back in the same attribute in the same object article.date. This feels more or less right to me, as article.date is only a String for one operation before being parsed and stored as a Date/'Moment' type.
The part I'm a little unsure of comes next. This is part of an ExpressJS app, and so an array of these objects is getting passed in as the data in a render() call, where it goes to a Jade template for rendering. But what if I want to use a display method in Moment.js to control the way the date looks as a String? Would it be reasonable to change the type of the date attribute back to a String again before passing it in?
Example:
articles[i] = processArticle(content);
// creates an article object from YAML, object has a property article.attributes.date
articles[i].attributes.date = moment(articles[i].attributes.date);
// attribute is now a Date/Moment
articles[i].attributes.date = articles[i].attributes.date.format("dddd, MMMM Do YYYY, h:mm:ss a");
// attribute is now "Sunday, February 14th 2010, 3:25:50 pm"
The reason why we even have type safety is to find errors in code early on: preventing invalid memory access/illegal operations/etc. In object orientated languages to allow decoupling and code re-use (polymorphism). Not just to make your life as a programmer more difficult, but to make sure your program will run.
Because JavaScript does not provide type safety, it's up to the programmer to do this. You will have to make sure that operations on a variable are valid and do not lead to an exception which stops your program from running. You will have to make sure that a method call can be invoked on any object.
So to answer your question: no, it's not a good practice to change the type of a variable as a function progresses.
To use Moment.js in your Jade template: How do I display todays date in Node.js Jade?

Categories