I have a registration form that asks the user for their start date. I am using a MEAN Framework which means Im using AngularJs on the front end.
What I have done so far:
First I tried using the following code BUT it does not work on IE & FireFox.
<input type="date" name="startDate">
Then I tried the following REGEX that I found on this SO question.
^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$
The works perfectly. Below is an image of how it works in my app.
The Issue:
When the user submits the form the data is saved in MongoDB. In the above image you can see that data is saved as a string. I want to convert it to a proper Date format so I can perform actions on it server side (Node.js) i.e filter all users who started after 2015 or left between 1999 & 2001 etc.
Any suggestions would be helpful.
UPDATE:
Currently I am testing moment.js. Will update my question once I have a result.
Result:
I tried the following using moment.js:
console.log("MOMENT" + moment("1993-03-25").format());
The output of the following was:
MOMENT 1993-03-25T00:00:00+00:00
I am not sure whether this is the correct format to be saved in MongoDB and will it allow me to perform server side actions on it.
You need to convert your moment object to a JavaScript date before you can save it MongoDB as a date field:
moment("1993-03-25").toDate()
Related
The default timezone in the laravel application is UTC and I'm creating a chat application that needs to show the user the time they are sending and receiving messages. Javascript seems to be very easy to convert the database time to the user's local timezone, I used the moment.js library to do this conversion in Javascript but now I don't want to use Javascript I want to use PHP to do the conversion. The problem with using PHP is that it is not converting the time to the actual user timezone, for example, my timezone is UTC+1 but I can only use UTC in PHP.
This is what the Javascript code looks like:
let time = moment.utc(data.created_at).local().format('YYYY-MM-DD HH:mm:ss');
And this is what the PHP code looks like:
{{date('H:i', strtotime($data->created_at))}}
I want to convert the database time to the user's local time using the user's timezone. The Javascript code works fine but the PHP is not working fine. For the UTC+1 the PHP code is an hour slow.
Don't rely on strtotime() or similar methods that "magically" know what format your date is. They guess. Educated guesses, but still guesses.
Use PHP's family of DateTime classes that are properly aware of timezones without mucking about with system-level settings.
$in_str = '2022-07-13 12:34:56';
$in_date = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $in_str, new DateTimezone('UTC'));
$out_date = $in_date->setTimezone(new DateTimezone('America/Chicago'));
var_dump(
$in_date->format('c'),
$out_date->format('c')
);
Output:
string(25) "2022-07-13T12:34:56+00:00"
string(25) "2022-07-13T07:34:56-05:00"
Lastly, I think you might have it backwards. You should be formatting the date/time for human eyeballs as the last thing before displaying it, eg: in Javascript. Everything for backend computation/storage [eg: PHP stuff] should ideally be in a single timezone, and that timezone should ideally be UTC for consistency's sake.
Javascript also has the benefit of running in the browser where it can access the user's timezone preference which is notably not sent in HTTP requests by default. If you want PHP to know about it, you'll have to write some Javascript to send it anyways.
So I'm making request to Slack api to get messsage history of a particular channel. Everything works fine except the messages that are returned are still in slack format. Is there any way to convert it to HTML format?
Thanks.
No. The Slack api will give you the messages in Slack markup only. If you want them in html you need to convert them yourself or find a library.
Here is how to parse the Slack markup in order to convert it into another format like HTML:
If you're retrieving messages, we've included some extra details in
the sections above to help you parse the formatting syntax. This will
let you properly format it for display on a different service, or to
help your app fully understand the intent of a message. Here are the
general steps involved for detecting advanced formatting syntax:
Detect all sub-strings matching <(.*?)>
Within those sub-strings, format content starting with #C as a channel link
Format content starting with #U or #W as a user mention
Format content starting with !subteam as a user group mention
Format content starting with ! according to the rules for special mentions
For any other content within those sub-strings, format as a URL link
Once the format has been determined, check for a pipe (|) - if present, use the text following the pipe as the label for the link or
mention.
From official Slack documentation on Formatting.
I'm trying to learn from this example:
http://fullcalendar.io/js/fullcalendar-2.9.1/demos/timezones.html
Please also right click on the web page to view the page source.
The only part that does not work now is as follows:
// when the timezone selector changes, dynamically change the calendar option
$('#timezone-selector').on('change', function() {
$('#calendar').fullCalendar('option', 'timezone', this.value || false);
});
It is trying to change the calendar time display based on the selection of different time zones.
I'm not an expert in Javascript and have tried to find FullCalendar's explanations on this but with no luck.
My question:
1) What can "this.value" possibly mean? "|| false"?
2) In the backend of my web application, I use Java web servlet with hibernate 3.6. For the time input (start time and end time), I use LocalDateTime from Java 8 and store it as UTC timestamp in MySQL. Not sure if this is the best practice for a calendar-based scheduler?
3) Currently, I don't have a timezoneId attached to the timestamp stored in the database yet. But I have created a list for the user to choose from as the FullCalendar example did. Any suggestion on what is the best way to store and later use a timezoneId for this calendar display across different time zones?
For now, I'm thinking that trying to totally imitate FullCalendar's example is not very practical because my database is different from theirs. I'm open to any good suggestions on how to get user's current timezone from his selection, store it in database and later display the proper time value based on any timezone selected by the user on his calendar view page.
If you inspect the timezone select you can see that each option has a value. this.value means the on change function will grab the value of the selected option to assign as the timezone. As far as storing your times, they should be stored as UTC timestamp like you are doing. For retrieving the timezone I use jstz to get the users timezone, its as simple as these two lines.
var tz = jstz.determine();
var timezone = tz.name();
This is great because you dont have to ask the user for their timezone. Then when I query my database for events I pass in the timezone
events: {
url: "calendarManager.php?request=get&timezone=" + timezone + "&userid=" + userID
},
and use php to convert from utc to the users local timezone and return the json encoded events.
I have a device that hosts a simple web server and records data on a job site in .csv format. We would like to display this data in a simple line graph to make it easier to interpret when logged in remotely. The device currently records the data as follows:
YYYY/MM/DD,HH:MM:SS,Data1,Data2,Data3
When using Dygraph I know that the date and time need to be in the first column in order for it to parse the data correctly. There is no way to change the format that the device uses to save the data, but is there a way to make Dygraphs use both columns as date and time?
How about replacing the first comma on every line with a space?
data = data.split('\n').map(line => line.replace(',', ' ')).join('\n');
You'll have to either change the format coming from the .csv file, or issue the XHR yourself (jQuery and other libraries can help) and transform the data before handing it off to dygraphs.
Right now I'm using one js file containing some function of that calendar code but my calendar always showing current date as client date. I tried a lot but finally I knew that calendar of javascript always take the client date and time .So can you please tell me how my web application calendar will show me the current date as server current date instead old client current date
As you know that js always take client side date so you have to make trick for it
one trick for which i go is
on the time of rendering html i will take server side date into an hidden field and when jquery calll the current date i will pass it as a current date
You could send a value from the server which holds the current server time and create a new date object on the client side which holds that value, hence effectively showing the date on the server.
However, there has to be a VERY good reason for doing so, as this can be very confusing to the user.
Also, your server may be located in one timezone right now, and another tomorrow (if you move to another provider), so the dates may be inconsistent if you save a response from the user.
If you grow a bit bigger and need redundancy across data center, you will definitely have different time zones as you will want your data centers to be distributed geographically.
So, what's usually a best practice is to show the user his own time, and translate it to whatever you want on the server.
you may simply read it a variable during page load like:
$startOfMonth= date("01-m-Y");
and assign it yo your datepicker field like
">
Please Try this
$( ".selector" ).datepicker({ defaultDate: "<?php date("m-d-y"); ?>" });