I'm going to make a time display using javascript with the following conditions, how can I do this? [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
how do i make a time display using javascript with the following conditions below:
If the message is received today, only the "hour" will appear.
If the message is received yesterday, it will only appear yesterday and the hour.
If the message is received this week, it will only display the day and time.
If the message is received in this year, it will only appear date and month only without years.
if the message is received last year or previous year, it will appear date-month-year.
example :
08:00
Yesterday 08:00
Monday 08:00
23 Jul
23 Jul 2016

For something like this, I would highly recommend using Moment.js.
console.log(moment("20111031", "YYYYMMDD").fromNow()); // 7 years ago
console.log(moment("20120620", "YYYYMMDD").fromNow()); // 6 years ago
console.log(moment().startOf('day').fromNow()); // 8 minutes ago
console.log(moment().endOf('day').fromNow()); // in a day
console.log(moment().startOf('hour').fromNow());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
You can find more information here.

Related

Can someone please explain how those lines code works? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Can someone please explain how those lines code works?
setInterval() executes function over and over. This function:
Declares variable birthday with future date as a reference and
converts it to special format (milliseconds since UNIX epoch)
Declares variable now with current moment converted to same special
format.
Declares variable distance with difference between above two.
That is, distance holds milliseconds left to birthday.
Populates DOM elements (days, hours, minutes, seconds) with time left to birthday
When difference becomes zero, it populates DOM elements (headline) with "It's my birthday" text and makes it visible (sets its style attribute) and at the same time makes counter invisible.
it will continue running while the browser open, and will show :
It's my birthday
on 30 september 2021 at 00:00:00

display time and date in GMT or UTC [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am trying to display GMT/UTC time with current date LIVE
i can display current time with it will be fixed until did refresh for the page
what i need to do display the time and date in UTC/GMT in my page as PHP or java code
thank you
If you want to convert your current time to another timezone lets say GMT or UTC then you can follow the following steps for javascript:
You can use moment-timezone lib.
const moment = require('moment-timezone')
const now = moment().format()
moment.tz(now, 'UTC').format()
moment.tz(now, 'GMT').format()

I want to create a script which tells me when a post is posted [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to create a script in php, javascript or jquery which tells me when a post is posted like Facebook in facebook when we post a status it tells us posting time like 5 minute ago or so on. I have completed all other work.
Create a coloumn to the table where you save the post details as posted_at and have DATETIME as its data type,
Use $CurrentTime = date("Y-m-d H:i:s" ); and insert while you create the post i.e., insert
So while retrieving the data just find the difference between the posted time and current time by
$PostedTime = strtotime($FetchedData->posted_at) - strtotime(date("Y-m-d H:i:s" ));
Then you shall convert the difference time as per your wish.
You have to create function that will get one parameter "posted date" and it will calculate difference from today. it will generate string and return like "2 days ago/5 min ago" or date. it is depend on you.

javascript count backwards 23 week days from current day for stock tracking [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am working on an application to get historical stock data from yahoo finance csv api. I am having trouble creating a script that will get the current day and count back 23 weekdays (not counting weekends or holidays). The idea is that it will plug the two dates in at yahoo, get the historical data and then create a moving average. I am unsure how to do this using the date function though.
You could use the Moment.js library, if you are able to provide that to your page. You could use it to do something like this:
var today = moment();
var pastDate = today.subtract('days', 23);
// then do something with those two variables...
if you need them in a certain format before you pass them along, just call .format on them
getHistoricalData(today.format('YYYYMMDD'), pastDate.format('YYYYMMDD'));

Digital clock in website [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can anyone please explain me how to put a digital clock in my jsp page. I have to put two clock one for India and one for Switzerland.
How can this be possible ?
This website has a tutorial on using the Date.getTimezoneOffset() method which you'll be able to use to define the clock locally.
http://www.onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/
India is easy- one time zone for the whole country, and no daylight savings time.
India time is UTC+05:30.
Sweden observes dst between 2:00 am on the last Sunday in March and 3:00 am the last Sunday in October.
Sweden's offsets are UTC+1:00 during standard time, UTC+2:00 during dst.
Be sure to apply the dst changes in Sweden's time, which is easy to calculate from UTC.

Categories