Display specific date and time format in Javascript - javascript

I have the following code to Display Date and time
**<div id='time'></div>
<script type="text/javascript">
// get a new date (locale machine date time)
var date = new Date();
// get the date as a string
var n = date.toDateString();
// get the time as a string
var time = date.toLocaleTimeString();
// find the html element with the id of time
// set the innerHTML of that element to the date a space the time
document.getElementById('time').innerHTML = n + ' ' + time;
</script>**
which display it as Wed Feb 09 2022 5:20:44 PM
what am looking for is how to get it display as :
Wednesday 9 February, 05:20 PM
appreciate d your assistance
Thank you

Here is a simple solution.
Note the comma is after the weekday name as this is the correct way of writing it. But it can be easily changed to be after the month name.
<div id='time'></div>
<script type="text/javascript">
// get a new date (locale machine datetime)
let date = new Date ();
let out = date.toLocaleString("en-GB",{weekday:'long',day:'numeric',month:'long'}) +" " +
new Intl.DateTimeFormat("en-GB",{hour12:true,hour:'numeric',minute: 'numeric'}).format(date);
document.getElementById('time').innerHTML = out;
</script>

Related

Json utc time to local time

I discovered that this URL api-sunrise-sun sunset gives date time in UTC format however I want to convert on the fly to local time irrespective where my webpage is opened ! How would I achieve that?
I suppose you are showing the time after successfully getting the json. Try this:
//today's date, or whatever
var t_date = new Date().toLocaleDateString();
//time from json. example: 10:04:34 PM
var t_from_json = response["results"]["sunrise"]
var utc_date = new Date( t_date + " " + t_from_json + " UTC")
//Date 2017-02-21T22:04:34.000Z
//two options:
utc_date.toString();
//"Tue Feb 21 2017 19:04:34 GMT-0300 (CLST)"
var offset = new Date().getTimezoneOffset();
utc_date.setMinutes(utc_date.getMinutes() - offset);
//Date 2017-02-21T19:04:34.000Z

Convert HH:MM input value to javascript valid date

I have an input value of a time specifically formatted HH:MM is there way I can convert it to a javascript valid date for example:
Time is: 10:20
Valid javascript time would be: Mon Aug 15 2016 10:20:00
var timeString = "10:20:51";
var timeStringArray = timeString.split(':');
var hours = timeStringArray[0];
var minutes = timeStringArray[1];
var seconds = timeStringArray[2];
var now = new Date();
now.setHours(hours);
now.setMinutes(minutes);
now.setSeconds(seconds);
console.log(now);
This just changes the hours/minutes component of the current time though

See if date selected is future from today - JavaScript

I have to compare the date that they want to put in and the current date today, and if they have put in a date that is in the future, then alert them to change the date, otherwise insert the data.
Basically I am having issues comparing the dates. here is my code:
var today = year + '-' + month + '-' + day + ' 00:00:00';
var d1 = new Date(postdate); // postdate = 2014/02/01 ie: 1 Feb 2014
var d2 = new Date(today); // todays date
if(d1>d2){
alert('You cannot post in the future!');
}
But that doesnt seem to work. Where am I going wrong?
Convert the dates into a comparable number, like milliseconds.
if(d1.valueOf()>d2.valueOf()){
alert('You cannot post in the future!');
}
You don't need to create a new variable today.
If by today you are trying to get today's date, you can simply do
var today = new Date();
var d1 = new Date(postdate); // postdate = 2014/02/01 ie: 1 Feb 2014
//----------
var d2 = new Date(year,month,day); // todays date
//----------
if(d1>d2){
alert('You cannot post in the future!');
}
Remember month is 0 based index. So, for december it would be 11.
Compare the dates with the same format, if today is 2014-01-24 00:00:00 then postdate also should be 2014-02-01 00:00:00
Then use + prefix to compare milliseconds:
if(+d1 > +d2){
alert('You cannot post in the future!');
}

How can I add a day to user's input? [duplicate]

This question already has answers here:
How to add days to Date?
(56 answers)
Closed 9 years ago.
I have a textfield that inputs date in this format: yyyy-mm-dd, how can I add a day to that users input? I have the following code but it doesnt work...
users_date = document.getElementById('users_date').value;
var date = new Date(users_date);
var next_date = new Date();
next_date .setDate(date.getDate()+1);
document.getElementById('next_date').value = next_date;
The first problem is the format of the date in the second is like 'Mon Aug 05 2013 16:24:40 GMT-0500 (Hora est. Pacífico, Sudamérica)'
The second problem is that when the user input the fist day of the month like '2013-01-01' or '2013-08-01' it displays 'Sun Sep 01 2013 16:26:06 GMT-0500 (Hora est. Pacífico, Sudamérica)' ALWAYS
For example if user inputs 2013-01-01 I want another textfield to be 2013-01-02 or 2013-08-31 it displays 2013-09-01, how can I do that?
Thanks!!
ITS NOT DUPLICATE BECAUSE THE OTHER POST DOESN'T FORMAT THE DATE!!!!
Prior to ES5 there was no standard for parsing dates. Now there is a format that is a version of ISO8601, however it isn't supported by all browsers in use and is not typically used for user input.
Normally a format is requested or a "date picker" used that returns a specific format. From there, it's quite simple to parse the string to create a Date object:
// s is date string in d/m/y format
function stringToDate(s) {
var b = s.split(/\D/);
return new Date(b[2], --b[1], b[0]);
}
For ISO8601 format (y-m-d), just change the order of the parts:
// s is date string in y/m/d format
function isoStringToDate(s) {
var b = s.split(/\D/);
return new Date(b[0], --b[1], b[2]);
}
To add one day to a Date object, just add one day:
var now = new Date();
var tomorrow = now.setDate(now.getDate() + 1);
This should work:
var date = new Date(document.getElementById('users_date').value);
var next_date = new Date(date.getTime() + 24*60*60*1000); // adding a day
document.getElementById('next_date').value = next_date.getFullYear() + "-" +
(next_date.getMonth()++) + "-" + next_date.getDate();
Please, note that Date#getMonth() is zero-based. Hence, the increment.

convert custom date string to date object

How can I convert a string representation of a date to a real javascript date object?
the date has the following format
E MMM dd HH:mm:ss Z yyyy
e.g.
Sat Jun 30 00:00:00 CEST 2012
Thanks in advance
EDIT:
My working solution is based on the accepted answer. To get it work in IE8, you have to replace the month part (e.g. Jun) with the months number (e.g. 5 for June, because January is 0)
Your date string can mostly be parsed as is but CEST isn't a valid time zone in ISO 8601, so you'll have to manually replace it with +0200.
A simple solution thus might be :
var str = "Sat Jun 30 00:00:00 CEST 2012";
str = str.replace(/CEST/, '+0200');
var date = new Date(str);
If you want to support other time zones defined by their names, you'll have to find their possible values and the relevant offset. You can register them in a map :
var replacements = {
"ACDT": "+1030",
"CEST": "+0200",
...
};
for (var key in replacements) str = str.replace(key, replacements[key]);
var date = new Date(str);
This might be a good list of time zone abbreviation.
You can use following code to convert string into datetime:
var sDate = "01/09/2013 01:10:59";
var dateArray = sDate.split('/');
var day = dateArray[1];
// Attention! JavaScript consider months in the range 0 - 11
var month = dateArray[0] - 1;
var year = dateArray[2].split(' ')[0];
var hour = (dateArray[2].split(' ')[1]).split(':')[0];
var minute = (dateArray[2].split(' ')[1]).split(':')[1];
var objDt = new Date(year, month, day, hour, minute);
alert(objDt);

Categories