473,769 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert UTC to Date

Hi,

I want to convert an UTC time to a Date object in Javascript.

The UTC time we have is a string that looks like "1160720058.377 452373"
for example.

I've done it in Java but I'm not able to do it via Javascript, that
should be pretty similar...

I would really appreciate any help on this.

Thansk very much in advance.

Jan 10 '07 #1
3 17505
Pi
I found this code in an old project I did, hope it helps. You might
have to mess with it little first.

function timeToHuman(x){
var theDate = new Date();
theDate.setTime (parseInt(x)); //this would remove the
millisecond part of it.

var dateString = theDate.toGMTSt ring(); //guessing
you'll now have the date in string format here. To get individual
elements...

var arrDateStr = dateString.spli t(" ");
var month = getMonthNum(arr DateStr[2]);
var day = arrDateStr[1];
var year = arrDateStr[3];
var hour = arrDateStr[4].substr(0,2);
var minute = arrDateStr[4].substr(3,2);
var second = arrDateStr[4].substr(6,2);
if (x==0) return ("");
else return (day+ '/' + month + '/' + year + ' ' + hour +
':' + minute + ':' + second)

}

function getMonthNum(abb Month){
var arrMon = new
Array("Jan","Fe b","Mar","Apr", "May","Jun","Ju l","Aug","Sep", "Oct","Nov","De c");
var i;
for(i=0; i<arrMon.length ; i++)
{
if(abbMonth == arrMon[i])
return i+1;
}

return -1;
}
Lonifasiko wrote:
Hi,

I want to convert an UTC time to a Date object in Javascript.

The UTC time we have is a string that looks like "1160720058.377 452373"
for example.

I've done it in Java but I'm not able to do it via Javascript, that
should be pretty similar...

I would really appreciate any help on this.

Thansk very much in advance.
Jan 10 '07 #2
Lonifasiko wrote on 10 jan 2007 in comp.lang.javas cript:
Hi,

I want to convert an UTC time to a Date object in Javascript.

The UTC time we have is a string that looks like "1160720058.377 452373"
for example.
You did not define the unit you are using,
but if it is minutes since 19700101, try:

r = 1160720058.3774 52373
d = new Date(r*1000)
alert(r);
I've done it in Java but I'm not able to do it via Javascript, that
should be pretty similar...
Java has nothing to do with javascript, but the name.

Javascript counts time in miliseconds.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 10 '07 #3
In comp.lang.javas cript message <Xn************ ********@194.10 9.133.242>
, Wed, 10 Jan 2007 09:03:25, Evertjan. <ex************ **@interxnl.net >
posted:
>Lonifasiko wrote on 10 jan 2007 in comp.lang.javas cript:
>I want to convert an UTC time to a Date object in Javascript.

The UTC time we have is a string that looks like "1160720058.377 452373"
for example.
You should have given what Gregorian date that represents, preferably in
ISO 8601 form.

>You did not define the unit you are using,
but if it is minutes since 19700101, try:
seconds since 1970-01-01 00:00:00 GMT or UTC.
>r = 1160720058.3774 52373
d = new Date(r*1000)
alert(r);
That will truncate, rather than round, to integer milliseconds. For
rounding, add 5e-4 - and if any dates may be before epoch, check what
happens there.

Before the multiplication, javascript should round the string to an IEEE
Double.

MS = 1000 * "1160720058.377 452373"
Do = new Date(MS)

The Object Do represents 2006-10-13 06:14:18 UTC

It's a good idea to read the newsgroup and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6
news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 10 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
67390
by: Loi Tan Vo | last post by:
Hi, I have a text file that contains a date column. The text file will be imported to database in SQL 2000 server. After to be imported, I want to convert the date column to date type. For ex. the text file look like Name date Smith 20003112
19
7287
by: Lauren Quantrell | last post by:
I have a stored procedure using Convert where the exact same Convert string works in the SELECT portion of the procedure but fails in the WHERE portion. The entire SP is listed below. Specifically, I have a problem with this portion in the WHERE clause: DATEADD(Day,tblMyEventTableName.ReminderDays, @DateNow) Between CONVERT(smalldatetime,str(DATEPART(Month, @DateNow)+1) + '/' + str(DATEPART(Day, tblMyEventTableName.TaskDateTime)) + '/'...
4
5390
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and calculate days, months, and years. This is not for a college course. It's for my own personal genealogy website. I'm stumped about the code. I'm working on it but not making much progress. Is there any free code available anywhere? I know it...
1
6481
by: Matt | last post by:
I would like to convert a couple informix stored procedures to SQL Server stored procedures. I have no idea how to accomplish this. Here is an example of one of the procedures I need to convert. drop function mnaf_calc_calendar_quarter; CREATE FUNCTION mnaf_calc_calendar_quarter(pEndDate Date) --***************************************************************************** -- Name: mnaf_calc_calendar_quarter -- Description/Notes:
3
29039
by: jerry.ranch | last post by:
I have a need to convert simple dates (i.e. 02/14/2005) to a number, do some math, and convert back to a date. (in a simple query). The math involves adding or substracting days, and days of the week . I've used the weekday() function to convert dates to numberic days of the week (1-7) I've used cdbl (date) to convert a date to a serial number, but then I do math with the number and I can't seem to convert this back to a date.
2
5089
by: Franck | last post by:
Hi, 'm gettin mad about date conversion. Here is the point. Got and add-in for Excel which call functions from a web service (on a remote server) The remote server has regional settings set to "en-UK" and date to
1
4606
by: abcabcabc | last post by:
I write an application which can let user define own date format to input, How to convert the date string to date value with end-user defined date format? Example, User Defined Date Format as "dd/MM/yyyy" input as "01082003" convert to date value as, 01 Aug 2003 Example, User Defined Date Format as "yyyy,dd,MM"
17
71457
by: Terry Jolly | last post by:
New to C# ---- How do I convert a Date to int? In VB6: Dim lDate as long lDate = CLng(Date) In C#
4
39343
by: perryclisbee via AccessMonster.com | last post by:
I have dates of service for several people that range all over each month. ie: patient had dates of service of: 7/3/2006, 7/24/2006 and 7/25/2006. I need to create a new field via a query that will convert each of the records of these service dates to the first date of that month, with results showing: 7/1/2006, 7/1/2006, 7/1/2006. How would you place an expression on a query that will convert any given date to the first day of the month...
8
6056
by: deepak_kamath_n | last post by:
Hello, I have the following scenario: 1. My application receives the date from another application as a string 2. The other application is running in a different time zone as compared to my app. 3. I need to convert the received date string in to a date w.r.t my local time zone.
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5307
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3964
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3564
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.