Show clock with CST time in html page using JavaScript - javascript

How to show clock with CST time format in HTML page using JavaScript? It should show exact CST time in this format "02 Jul 2015 10:34:45 PM"

Not sure if there is a better way but i was unable to find anything. This works though.
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sept", "Oct", "Nov", "Dec"
];
d = new Date();
//set the offset
var utcOffset = -6;
//get utc
var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
//create new date adjusting by the utcOffset to get local date/time
var localDate = new Date(utc + (3600000 * (utcOffset+1)));
//check if one character if so format as two by adding leading 0.
var formatDay = (localDate.getDate().length > 1) ? localDate.getDate() : '0' + localDate.getDate();
//string everything together
var fullDate = formatDay + ' ' + monthNames[localDate.getMonth()] + ' ' + localDate.getFullYear() + ' ' + localDate.toLocaleTimeString();
$('#div1').html(fullDate);
http://jsfiddle.net/ojga6a5u/

Related

Not able to convert String BST date format to UTC in javascript

I have a string like 07-Oct-2019 11:02 in BST format .I have to convert into UTC time in same format which will be 07-Oct-2019 10:02.
I have tried many ways and tried to find some inbuilt function but nothing worked .
I tried to write a function which will split string and then convert it into date and then UTC but that also did not work .
This is what i have tried
var fullDate = "07-Oct-2019 11:02";
fullDate = fullDate.split(' ');
var date = fullDate[0].split("-");
var time = fullDate[1];
var newDate = date[0] + '-' + date[1] + '-' + date[2] + ' ' + time;
var k = new Date(newDate);
alert(d);
But this result into current date into IST .
Please help how i can do this .
I am using DOJO framework .
Use moment-timezone to do advanced operations with date and timezone.
Solution:
let date = "07-Oct-2019 11:02"
console.log(
moment.tz(date, "DD-MMM-YYYY HH:mm", "Europe/London").utc().format("DD-MMM-YYYY HH:mm")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.16/moment-timezone-with-data.min.js"></script>
var date = "07-Oct-2019 11:02";
var a = date.split("-");
var b = a[2].split(" ");
var dateFormat = a[0] + ' - ' + a[1] + ' - ' + b[0] + ' ' + b[1];
var convertedDate = new Date(dateFormat);
weekdayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var dateString = convertedDate.getDate() + "-" + monthNames[convertedDate.getMonth()] + "-" + convertedDate.getFullYear() +" "+ convertedDate.getHours() + ":" + ("00" + convertedDate.getMinutes()).slice(-2);
console.log(dateString);

Javascript Date.getTime() returns NaN in internet Explorer 11 only

I have a below JavaScript function which returns time from the full date.
function f() {
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sept", "Oct", "Nov", "Dec"
];
d = new Date();
var utcOffset = -6;
var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
var localDate = new Date(utc + (3600000 * (utcOffset + 1)));
var fullDate = localDate.getDate() + ' ' + monthNames[localDate.getMonth()] + ' ' + localDate.getFullYear() + ' ' + localDate.toLocaleTimeString();
var now = new Date(fullDate).getTime();
return now;
}
In all browsers it returns expected output but not for Internet Explorer 11, as it returns NaN.
Please suggest changes to get the expected output.

Logic to add random time next to date using Jquery

My code returns date in the format dd-MMM--YYYY (example: 13-Jan-2014). I want to add a random time next to it. Something like: 13-Jan-2014 03:00 PM or 13-Jan-2014 03:00 AM. What can I add to get the desired output? Thanks.
function randomDate(start, end) {
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}
function DateAndTimeFormate(date) {
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var date = new Date(date);
return date.getDate() + '-' + monthNames[(date.getMonth() + 1)] + '-' + date.getFullYear();
}
var datewithformate = DateAndTimeFormate(randomDate(new Date(0000, 0, 0), new Date()));
//Start date
$("#time").val(datewithformate)
You can check this-
function randomDate(start, end) {
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}
function DateAndTimeFormate(date) {
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var date = new Date(date);
var hours = date.getHours();
var t = hours >= 12 ? 'pm' : 'am';
var hour = (hours % 12).toString().length == 1 ? '0' + (hours % 12) : (hours % 12);
var minutes = date.getMinutes();
var minute = minutes.toString().length == 1 ? minutes + '0' : minutes;
return date.getDate() + '-' + monthNames[(date.getMonth())] + '-' + date.getFullYear() + ' ' + hour + ':' + minute + ' ' + t;
}
var datewithformate = DateAndTimeFormate(randomDate(new Date(0000, 0, 0), new Date()));
//Start date
$("#time").val(datewithformate)
https://jsfiddle.net/Luhtfzxj/3/
You already have a method which produces a random date time, it is only a question of formatting.
function randomDate(start, end) {
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}
function DateAndTimeFormate(date) {
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var date = new Date(date);
// get the time
var time = date.toLocaleString('en-US', { hour: 'numeric', hour12: true });
// to fulfill your requirements some further manipulations
var ampm = time.slice(-2);
time = parseInt(time);
if (time < 10) { time = '0' + time }
time += ':00 ' + ampm;
return date.getDate() + '-' +
monthNames[(date.getMonth())] + '-' +
date.getFullYear() + ' ' +
time;
}
Example is here.
function DateAndTimeFormate(date) {
var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];
var date1 = new Date(date);
date1.setTime(1332403882588);
return date1.getDate() + '-' + monthNames[(date1.getMonth() + 1)] + '-' + date1.getFullYear() + ' ' + date1.getHours() + ':' + date1.getMinutes();
}
You can use .setSeconds(x) method to add a random time with x is random number falls in range of 0 <= x < 86400. For example:
var date = new Date('13-Jan-2014');
// date = Mon Jan 13 2014 00:00:00 GMT+0700 (SE Asia Standard Time)
var randomSeconds = Math.floor((Math.random() * 86400));
// randomSenconds = 53135
date.setSeconds(randomSenconds)
// date = Mon Jan 13 2014 14:45:35 GMT+0700 (SE Asia Standard Time)
I think this javascript library http://momentjs.com/ is helpful for you to manipulate/format/... dates that you don't need to write long/complicated code like the code in your question. For example:
moment().format('MMMM Do YYYY, h:mm:ss a'); // January 13th 2017, 2:17:37 pm

Stackoverflow style date format

How to format the javascript Date object the way stackoverflow does it.
For example. Aug 23 '10 at 23:35
This is what I tried.
new Date(val.replace(' ','T')+'Z').toString().split('GMT')[0]
This works cross browser. But doesn't look neat.
function formatDate(date) {
var monthNames = [
"Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct",
"Nov", "Dec"
];
var day = date.getDate();
var monthIndex = date.getMonth();
var month = monthNames[monthIndex];
var year = date.getFullYear().toString().substring(2,3);
var hours = date.getHours();
var minutes = date.getMinutes();
return month+' '+day+" '"+year+' at '+hours+':'+minutes;
}
Try this:
var date = new Date();
var formattedDate =
(date.toLocaleString("en-us", { month: "long" })) + " " +
date.getDate() + " '" + (date.getFullYear() % 100);
var formattedTime = date.getHours() + ':' + date.getMinutes();
alert( formattedDate + " at " + formattedTime );
Here's a JSFiddle.

datetime formatting in javascript

When I generate a date with the following code
var date = new Date();
I get a date-time of the following form
Sat May 11 2013 21:54:23 GMT-0700 (PDT)
Can anyone tell me how to generate a date of the form below without using regex/string functions.
Sat May 11 2013 21:54:23
Try giving this a shot: http://blog.stevenlevithan.com/archives/date-time-format
It has a collection of Date format options that I believe would give you what you desire.
Ignoring the above Javascript library and doing it native:
var date = new Date();
date = date.toDateString() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
Does this work ok for you?
Javascript does not come with a date formatting library beyond the one specific format you see. You can either build your own by piecing together the exact pieces you want and adding the strings together or you can get a third party date formatting library.
If you want a 3rd party library, the Datejs library is pretty thorough. In that library, it would be:
Date.today().toString("ddd MMM d yyyy H:mm:ss");
You could, of course obtain all the component values from the built-in date object and then build your own string too.
Without adding a library, you'd have to write your own way to make that specific format:
function formatDate(date) {
function makeTwoDigits(val) {
var prefix = val <= 9 ? "0" : "";
return prefix + val;
}
var dayOfWeek = date.getDay(); // 0-6, 0=Sunday
var month = date.getMonth(); // 0-11
var day = date.getDate(); // 1-31
var year = date.getFullYear(); // 2013
var hours = makeTwoDigits(date.getHours()); // 0-23
var mins = makeTwoDigits(date.getMinutes()); // 0-59
var secs = makeTwoDigits(date.getSeconds()); // 0-59
var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
return days[dayOfWeek] + " " + months[month] + " " + day + " " + year + " " +
hours + ":" + mins + ":" + secs;
}
Working demo: http://jsfiddle.net/jfriend00/zu7Uz/

Categories

Resources