I am using a bootstrapdatetimepicker as well as moment.js
When i call the following
var starteventtime= ($('#startdatetime').data('DateTimePicker').date());
I get a date similar to
Wed Mar 27 2019 00:00:00 GMT+0000
I also have the option for the user to save their own format of the date. So any code will have to take this into consideration.
<?php
$datetimeformat = 'DD/MM/YYYY HH:mm';
?>
$(function() {
$('#startdatetime').datetimepicker({
format: '<?php echo $datetimeformat; ?>'
});
How do i parse this on the client side with javascript to get Mysql formatted date so when the data is serialized it will be in a format ready for insertion into the database like below
2019-03-27 00:00:00
I am quite certain whatever the date time picker returns is a Date object and not a string (if it isn't, use another picker, because this one is useless).
When you have the Date object, you can format that however you want to insert it into your database. For example with the answer given here: How to format a JavaScript date
You should look into converting your dates into a standard Unix timestamp. Its super easy in every environment you're working in (Mysql, php, javascript). For example, in javascript Date.now() returns the current time as a Unix timestamp. Look up how to do that for the other places you're working with time. With that as a standard, you can just pass around the timestamps for your dates without having to worry about formatting until you're ready to display them.
Thanks for the advice , i used
moment($('#startdatetime').data('DateTimePicker').date()).format("YYYY/MM/DD HH:mm"));
to convert it to an object that was in mysql format.
Related
I'm not so sure what's the proper way to insert date into the database, but I'm using new Date().
So I get date format like this when I query from the database:
2021-09-24T12:38:54.656Z
Now I realized that date format is not so user-friendly. So I'm trying to convert it if possible standard readable format like this:
Sept 25 2015, 8:00 PM
I tried using toLocaleString() to my date pulled from db but it won't work probably if I got it correctly pulled date from the db is already a string?
Is there a workaround for this so that I don't need to change how I enter date to my db?
const date = moment("2021-09-24T12:38:54.656Z").format('MMM D YYYY, h:mm a')
console.log(date)
<script src="https://momentjs.com/downloads/moment.min.js"></script>
use momentjs
You can find format method and apply as you desire
https://momentjs.com/
I think it is better to store in DB as UTC 00 value and you are doing it right now.
When you retrieve it, you will be getting a string value something like, 2021-09-24T12:38:54.656Z. On the UI you can easily convert it to date variable in JS using,
const dateVar = new Date("2021-09-24T12:38:54.656Z");
console.log(dateVar.toLocaleString());
If you want more date formatting other than the inbuild solutions like toLocaleString you can use date libraries like moment.js
You'd need to create a Date object from the timestamp string like this.
let date = new Date('2021-09-24T12:38:54.656Z');
Then you can use toLocaleString, toLocaleDateString, toDateString, or toString as you see fit.
The stored date looks like this:
...
"date_of_birth" : ISODate("1920-01-02T00:00:00Z"),
...
Using moment, it is formatted in the model (in order to populate the input for updating the document) like this:
AuthorSchema
.virtual('date_of_birth_update_format')
.get(function(){
// format in JavaScript date format (YYYY-MM-DD) to display in input type="date"
return this.date_of_birth ? moment(this.date_of_birth).format('YYYY-MM-DD') : '';
});
Retrieved from the collection and displayed, it displays as one day earlier like this:
01/01/1920
I would appreciate any help to resolve this.
The date from mongo is always in GMT, and your server might be in other timezone. You need to convert date to GMT before formatting.
var moment = require("moment-timezone")
AuthorSchema.virtual('date_of_birth_update_format').get(function(){
return this.date_of_birth ? moment(this.date_of_birth).tz('GMT').format('YYYY-MM-DD') : '';
});
The Z in the ISO 8601 format implies 'GMT' i.e. 1920-01-02T00:00:00+0000. Moment will take your timezone into consideration. If you are in the continental US, your time zone offset is -0400—-0800.
1920-01-02T00:00:00Z = 1920-01-01T6:00:00-0600 In Pacific Standard Time for example.
It depends on the time zone which you are in for example am in India so GMT for me is +5:30 so whenever I retrieve from db I would add up 5:30 to time so that it matches the date and to answer why it storing it a day before because it stores date in ISO format that's why
Same problem here. I was using EJS to show the date retrieved from the MongoDB database. I'm not sure if the problem is with EJS, but I solved it like this:
All I had to do was to set the timeZone to GMT in the front-end:
date.toLocaleString("pt-BR", {timeZone:"GMT", day: "numeric", month: "numeric", year:"numeric"});
I have very complex structure that I receive from server-side code.
This structure has many Date properties (of type Date).
These Date properties contain dates in UTC.
I need to convert all of them to Local.
Is there any way to do this in angularJS?
Instead of doing this one-by-one?
Maybe some global setting or options that will instruct angular to convert dates into Local automatically?
Thanks.
append " UTC" to the backend time and run that through new Date(). It'll give you the local time offset.
var backEndDate = "2016-10-20 10:00 AM" + " UTC";
console.log(new Date(backEndDate));
The first approach is change your web service to return the utc date using ISO 8601 format.
For example: "2016-10-07T22:01:00Z"
If the web service return's the date using this ISO is easy to represents the date in local time of user because the browser instances the date based on your current time zone.
For example: if i open my browser console and run this javascript code:
new Date("2016-10-07T22:01:00Z")
I will receive the date based on my time zone, that is GMT-0300.
Fri Oct 07 2016 19:01:00 GMT-0300 (SA Eastern Standard Time)
So, for angularjs code you just need write::
{{"2016-10-07T22:01:00Z" | date}}
i will receive this result:
Oct 7, 2016
Check the filter for date here
For use the filter correctly. For example:
{{"2016-10-07T22:01:00Z" | date: 'MM/dd/yyyy HH:mm'}}
The result is:10/07/2016 19:01
The second approach is convert the date from your web service for the ISO 8601 format.
Actually i had the same problem and the client web service sends me the date just like this: "2016-10-07 22:01:00"
So, i wrote a simple code to convert this date format to ISO 8601.
Need help to convert exactly from ISO Date string to Date:
I have an ISO string date: "2016-01-23T22:23:32.927".
But when I use new Date(dateString) to convert Date, the result is wrong:
var date = new Date("2016-01-23T22:23:32.927");
The result is: Sun Jan 24 2016 05:23:32 GMT+0700. It's not true. I want the date is 23 not 24.
Please help me. Thanks a lot!
You need to supply a timezone offset with your iso date. Since there isn't one, it assumes the date to be in GMT and when you log it out, it prints it in the timezone of your browser. I think that if you pass "2016-01-23T22:23:32.927+07:00" to new Date() you would get the value you are expecting.
JavaScript environments (browser, node,...) use a single timezone for formatting dates as strings. Usually this is your system's timezone. Based on the output you get, yours is GMT+0700.
So what happened:
The string you passed as ISO format to the Date constructor doesn't specify a timezone. In this case it is treated as UTC.
When you then output the date (I'll assume with console.log), it is converted to the timezone of your environment. In this case 7 hours where added.
If that doesn't suit you, you can change the way you output the date. This depends on what output you want, e.g.:
If you just want the UTC timezone again, you can use date.toISOString().
If you want to output it in another timezone, you can call date.getTimezoneOffset() and figure out the difference between both timezones. You'd then probably need to get the individual date parts and add/subtract the timezone difference accordingly. At this point you could consider using an existing library, taking into account their possible disadvantages.
If you're willing and able to add a dependency, I recommend using moment.js for this. It makes date handling in Javascript much more straightforward and a lot safer, and fixes your specific problem right out of the box.
To do this, 1st load it from a CDN, e.g. Moment.JS 2.14.1 minified. Then use it as follows:
var date = moment("2016-01-23T22:23:32.927");
console.log(date);
// output: Sat Jan 23 2016 22:23:32 GMT-0500
...i.e. your desired result :)
Here's a jsfiddle demonstrating this.
Use date.toUTCString()
it'll give you 23 instead of 24 as it Convert a date object to a string, according to universal time
EDIT : Btw, I have no idea why this question was marked as a duplicate. The answers in the original question does not work for me. i.e, getting wrong results and stuffs. Furthermore, none of the answers deal with phstc's dateFormat function. Do correct me if I'm wrong. Btw, I have solved this question. Do take a look at my answer.
I want to change a UTC datetime to my browser's timezone. I'm using phstc's dateFormat in pure javascript form. Let's say I convert a datetime of 2014-06-27 07:11:16 using a javascript Date() function. The result I got was
Fri Jun 27 2014 07:11:16 GMT+0800 (Malay Peninsula Standard Time)
Then when I use phstc's toBrowserTimeZone function, it still returns me the same datetime. I wanted to get something like 2014-06-27 15:11:16
Here is the code below:
var originalDateTime = new Date(`2014-06-27 07:11:16`);
alert(DateFormat.format.toBrowserTimeZone(originalDateTime,"yyyy/MM/dd HH:mm:ss"));
According to this statement in phstc's dateFormat page,
value = String representing date in ISO time (“2013-09-14T23:22:33Z”) or String representing
default JAXB formatting of java.util.Date (“2013-09-14T16:22:33.527-07:00”) or String representing
Unix Timestamp (Sat Sep 14 2013 16:22:33 GMT-0700 (PDT)) or javascript date object.
JS Date object should work but unfortunately, it didn't. Well, I got it fixed by changing the datetime to other formats stated above first before calling the toBrowserTimeZone() function. For example,
var originalDateTime = DateFormat.format.date('2014-06-27 07:11:16',"yyyy-MM-ddTHH:mm:ssZ");
var newDateTime = DateFormat.format.toBrowserTimeZone(originalDateTime);