to store date in mongodb - mongodb

I need to store date in mysql (without time). User inputs date in input box like yyyy-mm-dd, may be later fomat could change.
Could you please tell what is good way to store date in mongodb (without time), we'd use DATE type in mysql? Now whe I need to store date and time I use mongdb date type.
And store it like this:
$data['ADDED'] = new MongoDate(time());
And display them:
echo gmdate('Y-m-d H:i:s', $data['ADDED']->sec);
When I use only date I store them as string like: yyyy-mm-dd (I validate date before storing it to make sure it's correct date). I'll need to find by date something like this:
dateField <(>) somedate
Do you think it's acceptable to store date as string in mongodb? How do you usually store date in mongodb?

MongoDB does not have a DATE type. It instead has a ISODate type. This is just as good for storing "without" time as I will explain.
So you can use MongoDate like so:
$date = new MongoDate(); // Denotes today, just like the date() function
Now to store without time you can just fake it by making PHP only set a date, as such the default time will be 00:00:00 (I should note this does mean a time is actually stored just as 00:00:00):
$date = new MongoDate(strtotime('2012-04-23')); // strtotime for fun
And then you can query by just the date part like:
find(array('date' => new MongoDate(strtotime('2012-04-23'))));
And you can now query as though you don't have a time since the time will equal what you put in: 00:00:00.
Of course this is just one way of fixing it.

Related

Is there a way we can get date alone from a datetime field stored as string in MongoDB. We are using Spring MongoDB driver

Let's say I have an date attribute stored in the format dd/MM/YYYY HH:mm:ss i.e. 22/12/2021 10:15:23. Now I just want 22/12/2021 as a date value so that I can do date comparisons. I want this operation to happen at database itself so that I can do date comparisons as part of the query/aggregation itself. Please note I am using Spring MongoDB driver.
Getting date alone from a datetime string is not possible in MongoDB since it always returns an ISO date with time. As a workaround we can convert the time HH:mm:ss to 00:00:00 either by applying String operations on the string attribute or by using Arithmetic operations on the date. We will have to add a temporary field in the query which will hold the converted value using add field operations. And then we can compare them with a given date.

Turning a localized Date to a Timestamp in Firebase

I'm new to Firestore and since it doesn't seem like it has a native createdAt/updatedAt as part of a document, I'm creating them when I create the new document. Using a straight up Date() as the value for my initial dictionary that I save to Firestore obviously gives me a localized date/time – UTC -500, for example.
Firestore stores this as November 19, 2018 at 5:14:54 PM UTC-5
Is there a specific date format that Firestore likes in order to save something as a Timestamp and so that I will be able to sort on it later?
I tried using "yyyy-MM-dd HH:mm:ss Z" as the dateFormat from just printing out the value of Date() in a Playground. Would love some help here. Thanks!
A Timestamp object doesn't have a date format. It's just a measurement of a number of seconds since Unix epoch, plus some number of nanoseconds. It does not accept a formatted time. If you have a formatted time, you will have to parse it to get a Timestamp object in return.`
The date format you're seeing in the console is just the way the console is choosing to format it. If you want to format a Timestamp for display, you'll need to do that yourself.

Using Current Date Time in SAS

I am selecting the data from a table using a date string. I would like to select all rows that have a update time stamp greater than or equal to today.
The simplest way that I can think of is to put today's date in the string, and it works fine.
WHERE UPDATE_DTM >'29NOV2016:12:00'DT;
However, if I want to put something like today's date or system date, what should I put?
I used today(), but it returned all rows in the table. I am not sure if it's because today() in SAS refers to the date 1/1/1960? I also tried &sysdate, but it returned an error message seems like it requires a date conversion.
WHERE UPDATE_DTM > TODAY();
Any ideas? Your thoughts are greatly appreciated!
DATETIME() is the datetime equivalent of TODAY() (but includes the current time). You could also use dhms(TODAY(),0,0,0) if you want effectively midnight (or, for your example above, dhms(TODAY(),12,0,0) to get noon today).

Date Column Split in Talend

So I have one big file (13 million rows) and date formatted as:
2009-04-08T01:57:47Z. Now I would like to split it into 2 columns now,
one with just date as dd-MM-yyyy and other with time only hh:MM.
How do I do it?
You can simply use tMap and parseDate/formatDate to do what you want. It is neither necessary nor recommended to implement your own date parsing logic with regexes.
First of all, parse the timestamp using the format yyyy-MM-dd'T'HH:mm:ss'Z'. Then you can use the parsed Date to output the formatted date and time information you want:
dd-MM-yyyy for the date
HH:mm for the time (Note: you mixed up the case in your question, MM stands for the month)
If you put that logic into a tMap:
you will get the following:
Input:
timestamp 2009-04-08T01:57:47Z
Output:
date 08-04-2009
time 01:57
NOTE
Note that when you parse the timestamp with the mentioned format string (yyyy-MM-dd'T'HH:mm:ss'Z'), the time zone information is not parsed (having 'Z' as a literal). Since many applications do not properly set the time zone information anyway but always use 'Z' instead, so this can be safely ignored in most cases.
If you need proper time zone handling and by any chance are able to use Java 7, you may use yyyy-MM-dd'T'HH:mm:ssXXX instead to parse your timestamp.
I'm guessing Talend is falling over on the T and Z part of your date time stamp but this is easily resolved.
As your date time stamp is in a regular pattern we can easily extract the date and time from it with a tExtractRegexFields component.
You'll want to use "^([0-9]{4}-[0-9]{2}-[0-9]{2})T([0-9]{2}:[0-9]{2}):[0-9]{2}Z" as your regex which will capture the date in yyyy-MM-dd format and the time as mm:HH (you'll want to replace the date time field with a date field and a time field in the schema).
Then to format your date to your required format you'll want to use a tMap and use TalendDate.formatDate("dd-MM-yyyy",TalendDate.parseDate("yyyy-MM-dd",row7.date)) to return a string in the dd-MM-yyyy format.

Meteor / JS Dates

So I am trying to make a timesheeting app in meteor, creating projects and adding time entries. Why? it was all I could think of as a test app.
But, I'm more used to dealing with PHP, in PHP I would just store a date field with a time length. Right now, I'm wondering what's the best wat to deal with dates in Meteor.
Do… I do the same thing where I store a parsed string of the date, or is it a date time object? How would you deal with dates? (I'm only 3 hours into Meteor)
Meteor also includes the momentjs library which makes dealing with dates and times very easy. You get function to format and parse.
The best way to store your time is in a Date object. This is because in Mongo you will get the timestamp and its GMT deviation. Making the time TimeZone secure.
In order to manipulate and display times, use momentjs.
This community hackpad with recommended methods and packages for storing and using dates is pretty useful:
https://meteor.hackpad.com/Meteor-Cookbook-Using-Dates-and-Times-qSQCGFc06gH
The best way to represent dates on your collection documents is by directly using the Date object type. You can store Date objects directly into collection documents. If we are creating a document, we can generate a Date object as one of the properties supplied to the collection's insert() method.
I would suggest that you store the time in epoch. It will make it a lot easier to sort and search. Normally getTime() gets the time since the epoch in miliseconds but you can divide by 1000 to get the time in seconds.1
var d = new Date();
var seconds = d.getTime() / 1000;
To convert to the local date if you need it you can just
var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
d.setUTCSeconds(seconds);