473,320 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Convert seconds to a date string and vice versa

Hello,

I have a device which stores time in its flash memory as the number of seconds since 01/01/1970. One of my roles is to get/set its time. I would like to know the most efficient code to convert the time collected from the device to a readable date, then convert the updated date back to number of seconds since 01-01-1970 before sending it to the device.

Thanks!

Mart
Dec 1 '11 #1
2 2056
arie
64
I don't know if it's the most efficient way, but something like this should work:
Expand|Select|Wrap|Line Numbers
  1. CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
  2. DateTime zeroPoint;
  3. long seconds = numberOfSecondsReturnedFromTheDevice;
  4. DateTime myDate;
  5. try
  6. {
  7.    zeroPoint = DateTime.Parse("1/01/1970 00:00:00", culture);
  8.    myDate = zeroPoint.Add(new System.TimeSpan(seconds*1000)); //timespan in miliseconds.
  9. }
  10. catch(Exception ex)
  11. {
  12.    MessageBox.Show("Parsing of date failure: " + ex.Message);
  13. }
Dec 1 '11 #2
Thank you very much for your help!

Mart
Dec 2 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Byron | last post by:
Hello, I am a newbie and would like to know if it is possible to convert a string back to a dictionary? For example, I can convert a dictionary to a string by doing this: >>> names =...
2
by: Steve - DND | last post by:
Just wondering if anyone out there has any code to convert a plural word to it's singular form and vice versa. Most of our database tables are named in a plural fashion. When we go to create...
1
by: ABC | last post by:
How to convert a date string to datetime value with custom date format? e.g. Date String Date Format Result (DateTime value) "05/07/2004" "MM/dd/yyyy" May 7, 2004 "01062005" ...
1
by: kevcar40 | last post by:
Hi I have an excel worksheet that i am beginnning to import in to access for anaysis, the person who designed the worksheet has used input forms to gather the data unfortunlately the date field...
1
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...
16
by: Hugh Janus | last post by:
Hi all, I am using the below functions in order to convert strings to bytes and vice versa. I totally ans shamefully stole these functions from this group btw! Anyway, they work great but as...
9
by: fpvt2 | last post by:
In VB6, I can format a date string like the following: format("12/15/06","yymmdd") and it returns 061215. In VB.Net, when I do the following: sdate.tostring("yymmdd"), it gave me an error "Value...
2
by: Stefan Antonelli | last post by:
Hi, i have to convert several timestamps. The given format, eg "yyyy-mm-dd hh:mm:ss" has to be converted to an epoch string. Is there any proper way to do this? If not, i have to split the...
6
yabansu
by: yabansu | last post by:
Hi all, I think most of you probably know the two .NET framework functions, namely Encoding.GetBytes(string) and Encoding.GetString(byte), to convert string into byte array and vice versa. Now,...
3
by: Jef Driesen | last post by:
How can I convert a date string to a number (e.g. a time_t value or a tm struct)? I know about the strptime function, but then I have to know the format string. And that is a problem. I'm trying...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.