Is it possible to attach/include timezone abbreviation along with the timestamp value?. the below code returns the value something like this eg:2-16-2017 # 16:7:20
I would like to include timezone abbreviation into this value? any thoughts
var currentdate = new Date();
var datetime = (currentdate.getMonth()+1) + "-"
+ currentdate.getDate() + "-"
+ currentdate.getFullYear() + " # "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();alert(datetime)
Thanks,
Muthu
I don't know whether you can get timeZone abbreviations but definitely, you can get the offset.
var offset = new Date().getTimezoneOffset();
console.log(offset);
One more option is to use toTimeString() instead of manually concatenating the time part:
currentdate.toTimeString() //"01:19:10 GMT+0400 (SAMT)"
Related
How to covert a string to a date in Angular13?
For example:
str = '2022-06-09T22:00:00Z'; to a datetime mm/dd/yyyy --:-- --
If you are okay with using a library, I would recommend to use moment.js.
Here's a link!
Firstly, since we can not access the inbuilt functions from a string, we need to convert it to a date:
var date = new Date("2022-06-09T22:00:00Z")
Using moment.js:
moment.utc(d).format('MM/DD/YYYY HH:mm:ss') // -> 06/09/2022 22:00:00
Using standard in-built functions:
var formatted = ("0"+(d.getUTCMonth()+1)).slice(-2) + "/" + ("0" + d.getUTCDate()).slice(-2) + "/" + d.getUTCFullYear() + " " + ("0" + d.getUTCHours()).slice(-2) + ":" + ("0" + d.getUTCMinutes()).slice(-2) + ":" + ("0" + d.getUTCSeconds()).slice(-2);
If you don't want the time in UTC, you can simply puzzle it around to remove it.
new Date("2022-06-09T22:00:00Z")
You can use angular pipe date
<span>{{ str | date: 'mm/dd/yyyy h:mm:ss' }}</span>
StackBlitz
I'm currently doing a web based task. I've got current time in this format.
hh:mm:ss
var currentdate = new Date();
var datetime =
currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
document.write(datetime);
How can i get current time in this format:
hh:mm:ss tt
Where tt is milliseconds. [i.e., 09:46:17 89]
Do something like this:
var date = new Date();
var milliSeconds = date.getMilliseconds();
You can add milliseconds to your function, but remember, for milliseconds format must be
hh:mm:ss ttt
If you only want two units of milliseconds, you must make a rounding
var currentdate = new Date();
var datetime =
currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds() + " "
+ currentdate.getMilliseconds();
document.write(datetime);
Try this by using the getMilliSeconds() method:
var currentdate = new Date();
var datetime =
currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds() + " "
+ Math.floor(currentdate.getMilliseconds() / 10).toFixed(0) ;
document.write(datetime);
The Math.floor(currentdate.getMilliseconds() / 10).toFixed(0) will make sure that you are getting the millisecond in 2 digit format as expected i.e, tt
I got a function which converts my timestamp into a date and looks like this:
delivery: function(created) {
var date = new Date(created * 1000);
var formattedDate = ('0' + date.getDate()).slice(-2) + '/' +
('0' + (date.getMonth() + 1)).slice(-2) + '/' + date.getFullYear() + ' ,' +
('0' + date.getHours()).slice(-2) + ':' + ('0' + date.getMinutes()).slice(-2);
return formattedDate;
}
now I want to display the date in MM/DD/YYYY for the american user which come to my site, so I would like to implement an if-clause, which firstly looks where the user comes from and then display the date in MM/DD/YYYY if he is from america, and if he is from europe in DD/MM/YYYY
Well in the SAPUI5 documentation I just saw this:
// The source value is given as timestamp. The used output pattern is "dd.MM.yyyy HH:mm": e.g. 22.12.2010 13:15
oType = new sap.ui.model.type.DateTime({source: {pattern: "timestamp"}, pattern: "dd.MMM.yyyy HH:mm"});
but I don't really understand how it works if I do oType.formatValue(created); its not working so maybe someone with more experience can explain me where I have to put my timestamp which is stored under "created"
What's wrong with simply using
delivery: function(created) {
var date = new Date(created * 1000);
return date.toLocaleDateString() + " " + date.toLocaleTimeString();
}
exactly?
EDIT: For clarity, I've supplied the whole function, not just the return statement
I have a script that stores an action taken by a user. There's a column that contains datetime and originally I user NOW(), but that uses server time, which is a few hours off as compared to the user's actual time.
So I decided I'll use the time that I can get with JS. I've formatted it this way:
var now = new Date(),
isnow = now.getFullYear() + '-' + ('0' + (now.getMonth() + 1)).slice(-2) + '-' + ('0' + now.getDate()).slice(-2) + ' ' + ('0' + (now.getHours() + 1)).slice(-2) + ':' + ('0' + now.getMinutes()).slice(-2) + ':' + ('0' + now.getSeconds()).slice(-2);
I've tested and while the format works fine, the time is off by an hour. Is it because of the Daylight Savings Time? How do I get the actual local time for the user?
In your code wrote:
...('0' + (now.getHours() + 1)).slice(-2)...
Try to remove this plus one
Additional you can check if Day Savings Time with:
if (now.dst()) { alert ("Daylight savings time!"); }
Date.prototype.stdTimezoneOffset = function() {
var jan = new Date(this.getFullYear(), 0, 1);
var jul = new Date(this.getFullYear(), 6, 1);
return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}
Date.prototype.dst = function() {
return this.getTimezoneOffset() < this.stdTimezoneOffset();
}
Based at answer similar issue
You should use the toISOString() method to convert the Date object to the ISO-8601 standard format:
now.toISOString();
The ISO-8601 date format puts the time information into a universal form which includes optional timezone information (likely the source of your issues).
I need to output the current UTC datetime as a string with the following format:
YYYY/mm/dd hh:m:sec
How do I achieve that with Javascript?
You can build it manually:
var m = new Date();
var dateString = m.getUTCFullYear() +"/"+ (m.getUTCMonth()+1) +"/"+ m.getUTCDate() + " " + m.getUTCHours() + ":" + m.getUTCMinutes() + ":" + m.getUTCSeconds();
and to force two digits on the values that require it, you can use something like this:
("0000" + 5).slice(-2)
Which would look like this:
var m = new Date();
var dateString =
m.getUTCFullYear() + "/" +
("0" + (m.getUTCMonth()+1)).slice(-2) + "/" +
("0" + m.getUTCDate()).slice(-2) + " " +
("0" + m.getUTCHours()).slice(-2) + ":" +
("0" + m.getUTCMinutes()).slice(-2) + ":" +
("0" + m.getUTCSeconds()).slice(-2);
console.log(dateString);
No library, one line, properly padded
const str = (new Date()).toISOString().slice(0, 19).replace(/-/g, "/").replace("T", " ");
It uses the built-in function Date.toISOString(), chops off the ms, replaces the hyphens with slashes, and replaces the T with a space to go from say '2019-01-05T09:01:07.123' to '2019/01/05 09:01:07'.
Local time instead of UTC
const now = new Date();
const offsetMs = now.getTimezoneOffset() * 60 * 1000;
const dateLocal = new Date(now.getTime() - offsetMs);
const str = dateLocal.toISOString().slice(0, 19).replace(/-/g, "/").replace("T", " ");
With jQuery date format :
$.format.date(new Date(), 'yyyy/MM/dd HH:mm:ss');
https://github.com/phstc/jquery-dateFormat
Enjoy
I wrote a simple library for manipulating the JavaScript date object. You can try this:
var dateString = timeSolver.getString(new Date(), "YYYY/MM/DD HH:MM:SS.SSS")
Library here:
https://github.com/sean1093/timeSolver
Not tested, but something like this:
var now = new Date();
var str = now.getUTCFullYear().toString() + "/" +
(now.getUTCMonth() + 1).toString() +
"/" + now.getUTCDate() + " " + now.getUTCHours() +
":" + now.getUTCMinutes() + ":" + now.getUTCSeconds();
Of course, you'll need to pad the hours, minutes, and seconds to two digits or you'll sometimes get weird looking times like "2011/12/2 19:2:8."
Alternative to answer of #JosephMarikle
If you do not want to figth against timezone UTC etc:
var dateString =
("0" + date.getUTCDate()).slice(-2) + "/" +
("0" + (date.getUTCMonth()+1)).slice(-2) + "/" +
date.getUTCFullYear() + " " +
//return HH:MM:SS with localtime without surprises
date.toLocaleTimeString()
console.log(fechaHoraActualCadena);
Posting another script solution DateX (author)
for anyone interested
DateX does NOT wrap the original Date object, but instead offers an identical interface with additional methods to format, localise, parse, diff and validate dates easily. So one can just do new DateX(..) instead of new Date(..) or use the lib as date utilities or even as wrapper or replacement around Date class.
The date format used is identical to php date format.
c-like format is also supported (although not fully)
for the example posted (YYYY/mm/dd hh:m:sec) the format to use would be Y/m/d H:i:s eg
var formatted_date = new DateX().format('Y/m/d H:i:s');
or
var formatted_now_date_gmt = new DateX(DateX.UTC()).format('Y/m/d H:i:s');
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC