I am looking for a function to convert UTC timestamp to another timezone timestamp.
It needs two parameters,
utcTimestamp(1464891132) &
timezoneString("Asia/Kolkata")
function convertToTimeZone(utcTimestamp, timezoneString) {
//do some operations and
return "timestamp in given timezone";
}
I just checked link and found that there are some js libs, which solve that conversion problem, but now my problem is how can I import third party js to my mongoJS.
Or is there any patch or alternative to solve these problem in mongojs?
FYI I am using mongoJS from mongoshell....
Related
I'm currently working on a project in which I create fixtures with Alice-bundle to run tests to make sure my API-endpoints work properly. Everything works fine, except for the DateTime properties.
No matter what string I pass it, eg: <dateTime('2019-09-23 14:00:00')>, it always gives me the wrong date and time, usually something like: 1998-10-25T14:29:45+01:00.
Even using <dateTime('now')> does not work -- it gives me some pre-2000s date & time as well, while that's exactly what some examples I'd found do use.
A fixture could look something like this:
Path\To\Task\Entity:
my_task:
title: 'My tasks'
description: 'These are all the tasks just for me!!!'
startsAt: <dateTime('now')>
endsAt: <dateTime('now')>
createdBy: '#some_higher_user'
Ideally I just want to pass it a string so I can define both a Date and Time and make sure it works properly, in the right format.
And help would be appreciated.
Looking here https://github.com/nelmio/alice/blob/master/doc/advanced-guide.md#functions we read:
function can be a Faker or a PHP native (or registered in the global scope) function.
So I would recommend trying a PHP native function that creates a \DateTime object
<date_create_from_format ( 'Y-m-d H:i:s' , '2019-09-23 14:00:00')>
// or
<date_create('now')>
That's how it works. The <dateTime()> function takes an argument called $max. So what it does is create a date between a starting date (not sure which one, probably something in the 1900 range or so) and that $max argument.
I guess you will want to use <dateTimeBetween()> which takes a startDate and an endDate to create a fake date between them. So I suppose if startDate = endDate, then you'll get the desired fixed date.
Take a look at fzaninotto/Faker library documentation. It's the library used by AliceBundle to actually fake data. There you can see what DateTime related functions you can use.
The following code works to load a local, static JSON file:
var stories = require('../stories/stories.json');
Now I want to load a file based on a variable, e.g. do something like this:
var storiesPath = '../stories/stories.json';
var stories = require(storiesPath);
But this triggers an error:
Error: Cannot find module '../stories/stories.json'
at require (packages/modules-runtime.js:123:19)
at meteorInstall.server.main.js (server/main.js:7:15)
Is there any way to get this working? I assume that I could load my file via the Meteor http package instead but I'd rather not add another package if I can avoid it.
Thanks for your hints
Like I said in the comment, you can easily use a variable in a require, e.g.,
> var x = 'fs';
> require(x).readFile
[Function]
So that's not the problem you are dealing with. Are you sure your first case indeed works? It would be surprising. I think you might be running into project file layout issues, due to the use of a relative path. I would stay away from that. And fortunately you can quite easily by using an asset! You can put your json file in private/ in your project folder and then use:
const stories = JSON.parse(Assets.getText('stories.json'));
I am trying to use Cypress.moment to compare dates. The targeted website is in french so the date format.
Therefore, I intended to use moment.js method to switch fr locale.
Cypress.moment.locale('fr')
I should be able to do that as they say on cypress documentation :
Cypress automatically includes moment.js and exposes it as Cypress.moment
https://docs.cypress.io/api/utilities/moment.html#Syntax
Then,
const todaysDate = Cypress.moment().format('Do')
const currentMonth = Cypress.moment().format('MMMM')
cy.get('.date__title').should('contain', todaysDate)
cy.get('.c-title').should('contain', currentMonth)
But the assertion fails as cypress refuse to take into account the fr locale. It keeps comparing 'décembre' with 'december' for instance. Which fails obviously.
I am doing something wrong ?
in the same aforementioned thread, a working answer recently surfaced:
put this in support/index.js:
Cypress.moment.locale('de');
it worked brilliantly in my project
Unfortunately, at the time of writing, I think this is not possible with Cypress.moment.locale(), as you can see in this issue in their git repository.
As commented there moment.locale requires an import in addition to the standard moment import, that import is moment-with-locales.min.js and has not been included in Cypress.
Before I updated Rails, the default Time format the API was returning was yyyy-MM-dd'T'HH:mm:ss'Z'. Now it returns yyyy-MM-dd'T'HH:mm:ss:sss'Z'. I want to change it back due to apps that rely on the old time format. How can I change this PROJECT default format that Time::DateTime's to_s method returns?
Edit:
I should probably mention I started using the active_model_serializer gem. Not sure if that makes a difference. My guess is this change happened after the update from Rails 4.0.x to 4.1.x. Also I want to change this format for the project. This format changed between Rails versions. active_model_serializer uses to_s, so it makes more sense to override how to project sets this. I don't know where that is.
You can just use strftime:
Time.now.strftime("%Y-%M-%d %H:%m:%S%Z")
one of your gems is probably overriding the DateTime to_s method. you can just override it again with you own method back
Maybe you need this:
# config/initializers/time_with_zone.rb
module ActiveSupport
class TimeWithZone
def as_json(options = nil)
iso8601
end
end
end
You can augment the Ruby built-in Date::DATE_FORMATS and Time::DATE_FORMATS
DATE_FORMAT = "%m/%d/%Y".freeze
TIME_FORMAT = "#{DATE_FORMAT} %l:%M%P".freeze
format_defaults = {
default: DATE_FORMAT,
date_time12: TIME_FORMAT,
date_time24: "#{DATE_FORMAT} %H:%M",
}.freeze
Date::DATE_FORMATS.merge!(format_defaults)
Time::DATE_FORMATS.merge!(format_defaults)
I want to create a dynamic php date format. Example: show the time of the node if it was published today and only the day/month when older then today. I want this format to be available throughout Drupal like the other predefined date formats in Drupal (not just on theme level).
I found the (D7 only) hook for hook_date_format_types but even that one doesn't seem to allow for a callback where I could define this PHP logic.
Does anyone know which hook would make this possible? Or a module which does this?
In Drupal6, format_date() has the dates and times hardcoded. Also, format_date() does not allow callbacks, but it does allow a custom string. That is where you can apply a trick: instead of hardcoding the string in there, you call a function that returns a string.
function mydate_format($timestamp) {
$now = time();
if (($now - $timestamp) < (60*60*24)) {
return "H:i";
}
else {
return "d:m:Y";
}
}
print format_date($timestamp, 'custom', mydate_format($timestamp));
The second option is to re-define a date-timestamp, but that is both hackish and limited. Date-formats are defined with variable_get(), but don't pass the timestamp along; so your example of switching formats based on the value of timestamp is not possible this way.
In settings.php:
$conf['date_format_long'] = $conf['debug'] ? 'r' : 'l, F j, Y - H:i';
This will switch from one value to another, based on whether your settings.php has a flag "debug" set to TRUE or not. As mentioned: the use for this is limited, since you cannot get any context.
The third alternative is to use Date API which offers onlinle configurable time-formats. But that is both clumsy and insecure (inputting executable PHP in your database). It also depends on a very large module-set. And has the same downside as the first solution: you cannot use format_date, but must use a modified function call, instead of format_date(). See all the options at The Drupal.org date themeing handbook.
GOTCHA In all cases Drupal will not call this function for cached content. If you want to have the dates really dynamic you either need to avoid caching alltogether, or implement the date-formatting in clientside javascript.
TL;DR: You cannot have dynamic date-formats without changing some of the code on theme-level. Using a callback-function to generate the "custom" dateformat is the simplest, working solution.
You can use Date API module to add your custom date formatting. Date API module is inside the Date module. After enabling the Date API module you can go the path "admin/settings/date-time/formats/add" to add your custom format.
"admin/settings/date-time/formats/configure" is the path to configure date formats.
Have a look at these. Happy coding.
Thanks
RT
You can go to node.tpl.php(possibly in sites/all/themes/theme_name/node.tpl.php). Here yo can find $created variable, to reformat date you can use your custom function and change the $created as you want.After this all nodes will use your formatted dates.
Regatds,
Chintan.
Use the features module. Create a feature.
In the resulting feature module, on the [feature].module file, create a hook_nodeapi function and format the field with a conditional statement that takes into account the current date() for how the date will be displayed and feed it into the $node->content var.
And that is the way rockstars would do it (-;