I logged out the value stored in one of the documents(or notes in my case) in the date key, like this
console.log(notes[0].date)
The output that I got was
and when methods like getDate() or toDateString() didn't work, what should i be doing?
You will need to convert it into a javascript date object using new Date("your mongodb date string");
See: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Date for more infomation
If your dates are stored using the date data type you should manipulate them as data object in your code.
If your dates are not stored using the date data type you should migrate your data to store the date as date data type rather than string.
You then format the date, using the user’s LOCALE, only when you present it to the user.
Related
I am using JSON.strigify to convert JSON object to a string, but in the process, it is also converting the local date to UTC date hence returning the date in the following format.
"2017-04-30T18:30:00.000Z"
I don't want it to happen and want to keep the date as is.
You can try of this Options
new Date("2017-04-30T18:30:00.000Z");
It Gives you date format you want
I got stuck in my project, due to some doubts related to moment.js. I'll state some conclusions here I made during writing backend on project, and please can someone correct me if something is wrong?
For example, if I get datetime string from fronted, in format:
"THU 18 MAR 2017 09:20 AM", I should create moment object passing this string to it and corresponding token "ddd DD MMM YYYY HH:mm A" as passed string is not in standard ISO 8601 format.
var datetime = moment(datetimeFromFrontend, "ddd DD MMM YYYY HH:mm A");
Now I have moment object that can be formated in way I want, calling format() function on moment object.
If I want to do some manipulations with datetime generally (for example, compare to today's datetime, or compare only time part), is it mandatory to convert all manipulating datetimes, times or whatever to moment object with same format and using isBefore, isEqual and so on, or can I somehow compare them using >, <, <=, =< ?
If I need to compare (>, < etc) datetime or just time part with value retrieved from SQL database (which is DATETIME or TIME data type), should I pass both comparing values to moment object, convert them in same format and then do manipulations?
And how to save to SQL database column which is DATETIME, or TIME type? Should do some transforms from moment object to string using format()? SQL will automatically convert passing string to corresponding data type?
Example:
var now = moment();
I assume that "now" can't be passed to sql query directly as it is moment object, it should be converted to string (and rely on SQL automatic conversion from string/nvarchar to datetime) or should I save it as moment().toDate() ?
If I need to compare (>, < etc) datetime or just time part with value retrieved from SQL database (which is DATETIME or TIME data type), should I pass both comparing values to moment object, convert them in same format and then do manipulations?
You might want to lookup the docs for that
You have Query functions that can compare moments and even Date objects, for example, this is the entry for the .after function
moment().isAfter(Moment|String|Number|Date|Array);
as you can see, you can pass anything to .isAfter and it will do that job for you
moment().isAfter(new Date(2017, 2, 4))
// true
moment(new Date(2016, 2, 4)).isAfter(new Date(2017, 2, 4))
// false
Of course the easiest way to compare >, < or === is to get the timestamp using Date::getTime, but that's ugly code
And how to save to SQL database column which is DATETIME, or TIME type? Should do some transforms from moment object to string using format()? SQL will automatically convert passing string to corresponding data type?
I think it depends how you SQL works and how your server communicates to it, ORACLE, you should refer to any documentation about storing date objects inside your sql, eventually dates and formats are all about making unix-timestamps human readable/understandable.
I assume that "now" can't be passed to sql query directly as it is moment object, it should be converted to string (and rely on SQL automatic conversion from string/nvarchar to datetime) or should I save it as moment().toDate() ?
Ask yourself what kind of benefits you or anyone that may use you API will gain from getting agnostic Date objects from your DB rather then just plain strictly formatted strings?
Ok this is really bugging me.
I am developing a web app and I need to work with dates. When a date is displayed in a view, or whenever a date is entered into a form I need the format to be dd/mm/yyyy.
What data type do I choose for my SQL database columns which contain dates. 'Date' doesn't seem to work, do I use varchar?
But If I use varchar how do I use java script to perform arithmetic with dates.
Do I do some conversions server-side?
Please advise the best practices.
Also Im using laravel if theres any useful stuff already built in.
Date is the correct type to use in SQL DB.
To access the value use ISO date format "YYYY-MM-DD HH:mm:ss" .
You can create it from Java Date Object by using toISOString() method.
For easier time format conversion I can also recommend to check out Moment.js.
Best practice to save the date in MySQL table as date field only. Which saves the date string in YYYY-MM-DD HH:mm:ss format.
You need to make sure the following things.
Before inserting date into MySQL change the format of date string to YYYY-MM-DD HH:mm:ss.
When you retrieve the date from database convert the date string from YYYY-MM-DD HH:mm:ss to your desired format.
You can use SimpleDateFormat class in Java to convert the dates
format. Use format() function to format the date in desired and
parse() function to get the Java date object from string.
Reference: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You should use varchar2 in mysql.
You can retrieve that varchar type date in javascript and create date object.
var d=dbdate;
var date = new Date(d);
And you can perform all javascript functions on date.
Hope it will help.
Javascript Date() object supports separate entries of date by:
var date=new Date();
var y=date.getFullYear();//4 digits
var m=date.getMonth()+1;//0-11 digits, plus 1 for true state
var d=date.getDate();//1-31
var dateSQL=y+'-'+m+'-'+d;//i.e 2016-07-25
you can use MySQL filed as datetime and also insert datetime format but when you will show then process it as you want as like
when you insert value in table then you can also format it as like
<?php $mysqltime = date ("Y-m-d H:i:s", $phptime); ?>
where $phptime is your input variable
$str = suppose $row['date'] (mysql filed value)
date("d/m/Y", strtotime($str));
where $str your retrieve date filed
$localStorage.doctorDateTime.push({
fullDate : new Date(doctorDateTime)
});
I passed date string in new Date and then save it to local storage but when i retrieve it from local storage it showing me this format:
2015-01-01T13:41:18.300Z
while if console.log(doctorDateTime). it is showing right date string
localStorage stored data are strings only. If you are trying to store something that is not string type, an implicit type coercion is taken place.
However, it looks like depending on some lib implementation you are using, because what you got behaves like Date.prototype.toISOString(), while following code behaves like Date.prototype.toString():
localStorage.setItem("fullDate", new Date(doctorDateTime));
You'd better explicitly convert the Date object to a string in your desired format before set to localStorage.
But, you could still get the Date object back with the ISO time string:
var str = '2015-01-01T13:41:18.300Z';
var time = new Date(str); // you got the object back!
That's what happens when you do ( new Date() ).toString(), it's the string representation of the date as it's converted to a string when stored in Local Storage.
Store the timestamp instead, it's a number representing milliseconds from epoch, and not an object
$localStorage.doctorDateTime.push({
fullDate : ( new Date(doctorDateTime) ).getTime()
});
Local Storage's functionality is limited to handle only string key/value pairs.
Some browser will store objects, but it's not something you can rely on, you should be storing strings. The easiest would be to store the timestamp, and then run that timestamp through new Date when you get it from Local Storage to get a date object.
If you pass object to localstorage it will first do JSON.stringify of your object and then store it to local storage. So when next time you retrieve it, it will give you string value of date object. Try to do JSON.stringify(new Date()) you will have your date string. This is same string you are getting when you fetch next time.
Best solution is convert your date to timestap when you store it to local storage. And convert it to Date object when you are fetching from local storage.
LocalStorage only supports text. So it will always do JSON.stringify on your object before storing it.
I am querying data using OData, url looks like http://myurl.com/api/Customer?$filter=ResDate eq DateTime'2014-03-15T12:01:55.123'.
I'm getting date/time from jquery.datepicker instead of the static date and using moment.js to convert from DD-MM-YYYY to YYYY-MM-DDTHH:mm:ss in order to pass it to web service.
function convertDateToISOdate(date){
var newDate = moment(date,'DD-MM-YYYY').format('YYYY-MM-DDTHH:mm:ss');
return newDate;
}
Date returns from the function, is 2014-03-15T00:00:00.
Problem : 2014-03-15T12:01:55.123 is not equal to 2014-03-15T00:00:00, so there's no record selected.
What I want is , just to compare the date , not include time stamp.
Note : I can not change the format date/time at server side(Web service) because it's not belongs to me.
Any idea is much appreciated.
Your first call to moment(date,'DD-M-YYYY') is stripping the time information from the incoming data. try using moment(date) (no format) instead because momentjs recognizes your incoming date format intrinsically, without having to be told which format to use, and will correctly parse the H:M:S data, too.
MomentJS date parse information